Parallel tasks in Python: concurrent.futures
vinta.ws
vinta.ws
For instance, most numpy operations release the GIL, meaning that you can perform heavy computation on multiple threads simultaneously. Certain other C extensions do the same, including some bits of the standard library. The usual caveats apply about threading bugs, of course.
Another detail is that numpy linked to e.g Intel MKL will multithread some operations by default. Running multiple threads-in-threads is likely to cause slowdown.
Now we have asyncio and awesome libraries like aiohttp[1] you can get a much, much higher throughput than you'd ever achieve with threads with less code.
http://techspot.zzzeek.org/2015/02/15/asynchronous-python-an...
This means that the "frontend" of a service can be asyncio, allowing it to support features like WebSockets that are non-trivial to support without aiohttp or a similiar asyncio-native HTTP server [2], while the "backend" of the service can be multi-threaded or multi-process for CPU-bound work.
0: https://docs.python.org/3/library/asyncio-eventloop.html#exe...
1: https://docs.python.org/3/library/asyncio-eventloop.html#asy...
2: Flask-SocketIO, for example, requires that you use eventlet or gevent, which are the "legacy" ways of doing asynchronous IO: https://flask-socketio.readthedocs.io/en/latest/
I don't know what "a small amount" means. You are bound by hardware (cores, hyper-threading). It simply makes no sense to spawn 32 threads executing computation intensive code (no IO), on a machine with 2 cores.
If you want the child processes to shutdown when the main process goes down, you should be able to just set .daemon = True on the process objects before you start them.
If you want exceptions in the children thread to propagate up to the main thread and then handle, looks like you'd just need to send the exception across a queue or something in multiproccessing. In the new futures library, your future (wrapping the process) has a return value property you can try to access. If the child process ran into an exception, that exception would be raised in the main process when you tried to access that return value.
Or did I misunderstand the problem?
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
Now it will just die on Ctrl-C. For text filter programs it's a good idea to do the same for SIGPIPE too. with ThreadPool() as pool:
fut = pool.submit(foo)
print(fut.result())
My idea is that ctrl+c would now cancel child operations cleanly?I think futures are nicer for certain types of interactions. For example, futures 'return' actual values, so its nice for dispatching a task that you'll get a result from back. Futures also raise exceptions (when you try to inspect their results, if an exception occurred in the task). This might make for cleaner error handling code.
https://github.com/grpc/grpc/issues/13873
I suspect it's not the only Python library that will see issues if you are running it in the Future context.
C extensions, IO operations etc. always release the lock. In practice GIL is a problem only when it is profiled to be a problem.
Python is used a lot in the data analysis world and nobody cares about the lock, because a fraction of the CPU time is spent within the lock.
So you are saying it is a problem in Python but not in other languages? Which is exactly my point :)
The linked repo looks like some nice wrappers/decorators around the 'old' multiprocessing library to make it really easy to parallelize a bunch of function calls within a blocking function.
[1] -https://docs.python.org/3/library/concurrent.futures.html#pr...