[Python-ideas] fork

Nick Coghlan ncoghlan at gmail.com
Thu Jul 30 09:24:50 CEST 2015


On 30 July 2015 at 03:00, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> On Jul 29, 2015, at 18:46, Sven R. Kunze <srkunze at mail.de> wrote:
>> What I would like to be freed of as well is: pool management. It actually
>> reminds me of languages without garbage-collection.
>
>
> That's a good parallel--but that's exactly what's so nice about "with Pool()
> as pool:". When you need a pool to be deterministically managed, this is the
> nicest syntax in any language to do it (except maybe C++ with its RAII,
> which lets you hide deterministic destruction inside wrapper objects). It's
> hard to see how it could be any more minimal. After all, if you don't wait
> on the pool to finish, and you don't collect a bunch of futures to wait on,
> how do you know when all the thumbnails are created?

asyncio offers a persistent thread-or-process pool as part of the
event loop (defaulting to a thread pool). Using the
call_in_background() helper from
http://www.curiousefficiency.org/posts/2015/07/asyncio-tcp-echo-server.html,
you can write:

    for image in images:
        call_in_background(create_thumbnail, image)

And if you actually want to do something with the thumbnails:

    futures = [call_in_background(create_thumbnail, image) for image in images]
    for thumbnail in run_in_foreground(asyncio.gather(futures)):
        # Do something with the thumbnail

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list