You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the following example, if I run it and then comment out the put loop and then run it again, I get 99 printed.
def worker():
while True:
try:
item = q.get()
print item
except:
pass
finally:
q.task_done()
for i in range(3):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in range(100):
q.put(item)
Hello @graingerkid, I will try to reproduce your case.
But a possible explanation is that the last entry is repeated because it was not completely processed (it didn't get a corresponding task_done()), so indeed it restarts from where it stopped.
Your initial code does not have a join(), so there's a chance that the main thread exits before their children, and in this case the processing may be interrupted in the middle. You can force this case by adding a time.sleep(1) in the worker() function emulating some intensive processing.
def worker():
while True:
try:
item = q.get()
print item
except:
pass
finally:
q.task_done()
for i in range(3):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in range(100):
q.put(item)
q.join()
I'd put time .sleep(1) in as follows:
def worker():
while True:
try:
item = q.get()
print item
except:
pass
finally:
q.task_done()
for i in range(3):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in range(100):
q.put(item)
time.sleep(1)
q.join()
but you suggest within the worker() function? I'm just concerned about speed, though time.sleep(0.1) might be fine.
defworker():
whileTrue:
try:
item=q.get()
printitemtime.sleep(1) # emulate an intensive work hereexcept:
passfinally:
q.task_done()
Then, remove your last time.sleep(1) before q.join(). You would have to wait ~33 seconds if you keep range(100), so you can reduce to just 10 iterations by using range(10) and you will wait 3 seconds each time.
By repeating the execution, you would not see the item 99 repeating in the second run. If so, we may have a BUG in the code.
Activity
graingerkid commentedon Apr 25, 2017
I added a
time.sleep(1)before theq.join()and that seems to have fixed the issue.I guess it's terminated before it's completed the run.
balena commentedon Apr 25, 2017
Hello @graingerkid, I will try to reproduce your case.
But a possible explanation is that the last entry is repeated because it was not completely processed (it didn't get a corresponding
task_done()), so indeed it restarts from where it stopped.Your initial code does not have a
join(), so there's a chance that the main thread exits before their children, and in this case the processing may be interrupted in the middle. You can force this case by adding atime.sleep(1)in theworker()function emulating some intensive processing.graingerkid commentedon Apr 25, 2017
Yea this was my full code (sorry)...
I'd put
time .sleep(1)in as follows:but you suggest within the
worker()function? I'm just concerned about speed, thoughtime.sleep(0.1)might be fine.balena commentedon Apr 25, 2017
Try the following:
Then, remove your last
time.sleep(1)beforeq.join(). You would have to wait ~33 seconds if you keeprange(100), so you can reduce to just 10 iterations by usingrange(10)and you will wait 3 seconds each time.By repeating the execution, you would not see the item 99 repeating in the second run. If so, we may have a BUG in the code.
graingerkid commentedon Apr 25, 2017
It seems to work sometimes and then not others. Any ideas how we could narrow down the issue?
graingerkid commentedon Apr 25, 2017
FYI i've got the following set...
Queue("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/micro_service/queue_data", tempdir="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/micro_service/queue_data")balena commentedon Apr 28, 2017
@graingerkid, I think you already narrowed down the problem. I'm just without time to work on this issue, but I will certainly do a test case for it.