Taming the race-condition, like a boss
Widnyana
http://s.id/3cV
http://courses.cs.vt.edu/professionalism/Therac_25/Therac_1.html
ensure there are no two concurrent processes are in their critical section at the same time.
...
while len(stack) < count and _ret <= _maxret:
result = __inner(user_id, count=count + 2)
for feed in result:
#: lock disini. proses kalau dapet lock.
#: delete locknya setelah task selesai ditandai oleh worker.
unlockkey = lock_byid(feed['id'], worker_id, ttl=1 * 60)
if unlockkey:
logger.debug_log("pid: {} | lock succeed: {}".format(worker_id, unlockkey))
permitted_pid = redis.get(unlockkey)
if worker_id == int(permitted_pid):
rsspost_setstatus("processed", 1, feed['id'], "upd")
logger.debug_log("pid: {} | setstatus succeed. {}".format(worker_id, unlockkey))
stack.append(feed)
#: ok, now we do the unlock things
unlock_byid(unlockkey, worker_id)
logger.debug_log("pid: {} | unlock succeed. {} \n\n".format(worker_id, unlockkey))
if len(stack) == count:
break
#: incr counter
_ret += 1
itu sudah, tidak perlu pakai blekmejik. iya, serius