Live data from Hacker News

How to Make Python Wait

blog.miguelgrinberg.com

1–10 of 33 posts

Re: How to Make Python Wait

#3
post #2

Good article on an important topic for novices, but why is it so long ?

Because it helps you learn.

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

#6
post #2

Good article on an important topic for novices, but why is it so long ?

Actually, I started reading about async/await from asyncio package. But for a newbie the above article is much simpler to grasp concurrency and parallelism concepts. And the sample code makes it a lot easier.

Re: How to Make Python Wait

#8
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, 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

#9

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

That is true. Python has better ways to deal with concurrency. As I wrote elsewhere in comments, I started reading asyncio. But found that for a newbie this article is good to grasp basic concepts.

Re: How to Make Python Wait

#10

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 is solid advice.
Post reply on HN