agronholm / pythonfutures

Backport of the concurrent.futures package to Python 2.6 and 2.7
Other
232 stars 51 forks source link

Threading needs at least one statement after submit to start #35

Closed thorstenkampe closed 9 years ago

thorstenkampe commented 9 years ago

OS: Windows 8.1, Python 2.7.9 32-bit and 3.4.3 64-bit, also tested under Cygwin and Ubuntu 14.10

The following code snippet will return False on Python2 with pythonfutures and True on Python3 with the builtin module:

import time, concurrent.futures

executor = concurrent.futures.ThreadPoolExecutor(max_workers = 1)
future   = executor.submit(time.sleep, 1)
#print('')
print(future.running())

If you uncomment print(''), Python2 with pythonfutures will print True.

agronholm commented 9 years ago

Don't rely on "running". This is a natural race condition, not a bug.

thorstenkampe commented 9 years ago

There is no mentioning neither in your nor the Python documentation that "running" has a race condition. If I cannot rely on "running" then "running" is essentially useless.

agronholm commented 9 years ago

I'm not sure how you expected this to work. The "running" state is set by the thread pool executor. It's random which threads gets there first: your call of future.running() or the other thread that sets it.

thorstenkampe commented 9 years ago

If I understand you correctly, you're saying that future.running() returns false simply because the thread is yet not running. That might be the case but it does not explain the fact that the code snippet I posted is always returning true with Python3 and always returns false with your backport. I would expect a backport to be consistent with the original implementation.

agronholm commented 9 years ago

And how am I supposed to accomplish that? I hope you understand that the timing is beyond my control.