Live data from Hacker News

How to Make Python Wait

blog.miguelgrinberg.com

31–33 of 33 posts

Re: How to Make Python Wait

#31

Don'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,…

Great example. Can I ask how I would discover or remember this import line, in case I forget?

I often remember useful standard libraries that I hear about by name, after using them once, but this is a long one.

Alternatively - is this an example in the python readthedocs yet? I try to fall back to that when possible, so my code stays simple for others.

Re: How to Make Python Wait

#32

Don'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,…

Great example. Can I ask how I would discover or remember this import line, in case I forget? I often remember useful standard libraries that I hear about by name, after using them once, but this is a long one. Alternatively - is this an example in the python readthedocs yet? I try to fall back to that when possible, so my code stays simple for others.

I don't. I have a huge knowledge base I store on my computers with those kind of things. E.G: this snippet is actually almost verbatim a file I have on my laptop I wrote months ago and kept so I don't have to rewrite that again.

Re: How to Make Python Wait

#33

Don'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,…

This example still involves a lot of manual work. It's often times even easier. from concurrent.futures import ProcessPoolExecutor import string def hello() -> int: 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__": inputs = list(string.printable) results = [] # You ca…

The code needs some minor changes (imports, missing parameter and indent) to make it runnable.

  from concurrent.futures import ProcessPoolExecutor
  import string
  import random
  import time

  def hello(output) -> int:
    seconds = random.randint(0, 5)
    print(f'Hi {output} {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__":

    inputs = list(string.printable)
    results = []

    # You can sub out ProcessPool with ThreadPool. 
    with ProcessPoolExecutor() as executor:
        results += executor.map(hello, inputs)

    [print(s) for s in results]
EDIT: I also struggle with proper code indentation on HN.
Post reply on HN