How to Make Python Wait
blog.miguelgrinberg.com
How to Make Python Wait
1–10 of 33 posts
Re: How to Make Python Wait
#2Re: How to Make Python Wait
#3Good article on an important topic for novices, but why is it so long ?
You can tell someone a brief answer to memorize, but to teach them anything takes a little more explaining.
For example, Question #1 is often "how do I do this in some particular way", but 2 and 3, "why" and "when" should I do it that particular way -- they take more explaining than #1 -- and through that explanation, you start to get good at your craft.
Re: How to Make Python Wait
#4Re: How to Make Python Wait
#5Re: How to Make Python Wait
#6Good article on an important topic for novices, but why is it so long ?
Re: How to Make Python Wait
#7Good article on an important topic for novices, but why is it so long ?
Re: How to Make Python Wait
#8Manually dealing with threads and processes is useful if you want to build a framework or a very complex workflow. But chances are you just want to run stuff concurrently, in the background.
In that case (which is most people case), you really want to use one of the stdlib pools: it takes care of sync, serialization, communication with queues, worker life cycle, task distribution, etc. for you.
Plus in such case, waiting for results is super easy:
import time
import random
from concurrent.futures import ThreadPoolExecutor, as_completed
# the work to distribute
def hello():
seconds = random.randint(0, 5)
print(f'Hi {seconds}s')
time.sleep(seconds)
print(f'Bye {seconds}s')
return seconds
# max concurrency is 2
executor = ThreadPoolExecutor(max_workers=2)
# submit the work
a = executor.submit(hello)
b = executor.submit(hello)
# and here we wait for results
for future in as_completed((a, b)):
print(future.result())
Want multiple process instead ? It's the same API: import time
import random
from concurrent.futures import ProcessPoolExecutor, as_completed
def hello():
seconds = random.randint(0, 5)
print(f'Hi {seconds}s')
time.sleep(seconds)
print(f'Bye {seconds}s')
return seconds
# Don't forget this for processes, or you'll get in trouble
if __name__ == "__main__":
executor = ProcessPoolExecutor(max_workers=2)
a = executor.submit(hello)
b = executor.submit(hello)
for future in as_completed((a, b)):
print(future.result())
This is Python. Don't make your life harder that it needs to be.Re: How to Make Python Wait
#9Don't follow this blog post advice. Manually dealing with threads and processes is useful if you want to build a framework or a very complex workflow. But chances are you just want to run stuff concurrently, in the background. In that case (which is most people case), you really want to use one of the stdlib pools: it takes care of sync, serialization, communication with queues, worker life cycle, task distribution,…
Re: How to Make Python Wait
#10Don't follow this blog post advice. Manually dealing with threads and processes is useful if you want to build a framework or a very complex workflow. But chances are you just want to run stuff concurrently, in the background. In that case (which is most people case), you really want to use one of the stdlib pools: it takes care of sync, serialization, communication with queues, worker life cycle, task distribution,…