Live data from Hacker News

Python Concurrency Decorators

github.com

31–40 of 51 posts

Re: Python Concurrency Decorators

#31
post #29

A couple of things offhand: - It has Python 2 and 3 support - It's a wrapper for the Python built-in "multiprocessing" library - It spreads out work over all cores (so the abstraction hides the ability to control the pool) Seems like a great way to get your feet wet with multiprocessing in Python, but it likely has limited use in production...although certain infrastructures like resource limited containers might be…

Does anyone know how this library addresses the Global Interpreter Lock (GIL) issue, as multiprocessing really isn't that great/is worse than running on a single thread in many versions of python.

> the Global Interpreter Lock (GIL) issue

This applies to programs that run Python bytecode in multiple threads in the same process. Multiprocessing forks multiple processes, so there is no GIL issue.

Re: Python Concurrency Decorators

#32

So, for me, replacing imap with pool.imap is the easy part. The hard part is dealing with things like handling exceptions, catching keyboard interrupts, and so on. Does this module do anything to address these issues?

I have a hack for dealing with KeyboardInterrupt's on a ProcessPoolExecutor: https://github.com/tgbugs/desc/blob/master/util/process_fixe.... I used this in concert with asyncio run_in_executor which helps with some of the exception handling.

Re: Python Concurrency Decorators

#33
post #30

this is such a good idea! the @synchronized decorator to collect the parallelized task at the end of a parent call is very very smart & simple.

It also has a potential for slowing down your code if used willy nilly, of course. Multiprocessing gets pretty useless for anything outside of independent CPU bound tasks with little IPC and simple data types that can be stuck into shared memory. If you're using multiprocessing pools so often that you think you need a decorator to clean up your code, then wow, I'd like to see what you're up to. ;)

you guessed right that this isn't directly useful to my life. all my parallelism is already taken care of by a framework.

that said I really like the idea of using inner and outer function calls as a hook for spawning and collecting promises. it doesn't only have to be a join on a CPU-bound worker pool; this feels like a cleaner way to abstract IO waits than the yield statements I saw in an early prototype of tulip.

In general, this feels like a clean way to compose any library logic that involves an event loop or execution plan rather than just a function call.

Re: Python Concurrency Decorators

#34

Another page you might be interested in bookmarking: https://wiki.python.org/moin/PythonDecoratorLibrary A similar decorator to thread function calls for concurrency: https://wiki.python.org/moin/PythonDecoratorLibrary#Lazy_Thu...

A lot of these need to be updated. For one, they don't follow PEP8.

Secondly, unfortunately, the signal thing doesn't work on Windows and the threading thing is a bad example because it simulates multiplication, but if you actually had a CPU intensive task, there would be no performance benefits.

Re: Python Concurrency Decorators

#35

So, for me, replacing imap with pool.imap is the easy part. The hard part is dealing with things like handling exceptions, catching keyboard interrupts, and so on. Does this module do anything to address these issues?

I have a hack for dealing with KeyboardInterrupt's on a ProcessPoolExecutor: https://github.com/tgbugs/desc/blob/master/util/process_fixe... . I used this in concert with asyncio run_in_executor which helps with some of the exception handling.

That's a nice hack when you have only a few elements in the iterable, but Futures are pretty heavyweight, so this is unfortunately a lot slower than multiprocessing.Pool when you have many elements.

The other thing is that this won't work on Windows. Efficient multiprocessing always a pain in Python in my experience.

Re: Python Concurrency Decorators

#36

Earlier quoted context omitted.

Looks like there is: https://github.com/alex-sherman/deco/blob/cee63391bf4c6d66ee...

Interesting, so when they claim it automatically scales out to all cores what they mean is it defaults to 3 unless overridden.

Should really be multiprocessing.cpu_count() / 2 or something like that.

Re: Python Concurrency Decorators

#37
post #30

this is such a good idea! the @synchronized decorator to collect the parallelized task at the end of a parent call is very very smart & simple.

It also has a potential for slowing down your code if used willy nilly, of course. Multiprocessing gets pretty useless for anything outside of independent CPU bound tasks with little IPC and simple data types that can be stuck into shared memory. If you're using multiprocessing pools so often that you think you need a decorator to clean up your code, then wow, I'd like to see what you're up to. ;)

> If you're using multiprocessing pools so often that you think you need a decorator to clean up your code, then wow, I'd like to see what you're up to. ;)

.. and I think once you do that in Python you should probably use NumPy.

I've been thinking that there should be a way to just program kernels in Modern Fortran because it's the easiest to interact with NumPy data structures (NumPy is nothing but glue code around a collection of very efficient Fortran numeric code). f2py [1] is doing that basically, but I've never had the chance to setup a project like this.

[1] http://docs.scipy.org/doc/numpy-1.10.1/user/c-info.python-as...

Re: Python Concurrency Decorators

#38
post #36

Earlier quoted context omitted.

Interesting, so when they claim it automatically scales out to all cores what they mean is it defaults to 3 unless overridden.

Should really be multiprocessing.cpu_count() / 2 or something like that.

I found out recently that some tasks do better if there are slightly more processes than cores.

Re: Python Concurrency Decorators

#39

source looks surprising, for instance the decorators parse the decorated function code and build an AST (still have to find out why)

My best guess is that this is to circumvent issues with naming encountered by using decorated functions in conjunction with multiprocessing pools. Typically you would run into serialization errors.

it turns out that the decorator "rewrite" the fn code. that's at least how the synchronized decorator does its own magic. Once I saw that in the source code (and other few hacks) I was sure I was never going to use this lib :)

Re: Python Concurrency Decorators

#40

Another page you might be interested in bookmarking: https://wiki.python.org/moin/PythonDecoratorLibrary A similar decorator to thread function calls for concurrency: https://wiki.python.org/moin/PythonDecoratorLibrary#Lazy_Thu...

And here they are a few more decorators https://python.libhunt.com/categories/284-concurrency-and-pa...
Post reply on HN