Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Done Task Repeating #10

Open
graingerkid opened this issue Apr 25, 2017 · 7 comments
Open

Final Done Task Repeating #10

graingerkid opened this issue Apr 25, 2017 · 7 comments

Comments

@graingerkid
Copy link

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)
@graingerkid
Copy link
Author

I added a time.sleep(1) before the q.join() and that seems to have fixed the issue.

I guess it's terminated before it's completed the run.

@balena
Copy link
Owner

balena commented 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 a time.sleep(1) in the worker() function emulating some intensive processing.

@graingerkid
Copy link
Author

Yea this was my full code (sorry)...

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.

@balena
Copy link
Owner

balena commented Apr 25, 2017

Try the following:

def worker():
    while True:
        try:
            item = q.get()
            print item
            time.sleep(1)   # emulate an intensive work here
        except:
            pass
        finally:
            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.

@graingerkid
Copy link
Author

It seems to work sometimes and then not others. Any ideas how we could narrow down the issue?

@graingerkid
Copy link
Author

FYI i've got the following set...

Queue("/micro_service/queue_data", tempdir="/micro_service/queue_data")

@balena
Copy link
Owner

balena commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants