3 ms·
Honestly for python there's still one obvious way given just about all use-cases. Use multiprocessing for now. In the future a lot of stuff is going to shift o
by Doxin 6y ago
Honestly for python there's still one obvious way given just about all use-cases. Use multiprocessing for now.
In the future a lot of stuff is going to shift over to async-await. Until enough of your dependencies do it's fine if you ignore it for now.
If you need any of the other options you'll probably already know and don't need to ask.
Threading is good if you're doing a lot of waiting, but this use case is more or less replaced by async await.
Multiprocessing is good multi-purpose. It works around the GIL and so works for most use cases.
Async-await is like threading with a nicer programmer interface and works especially well for networking code and the like. The nice thing about async-await is that it's trivially interoperable with all the previous options. When using a framework or library that has an async-await API it'll be doing whichever form works best on the backside. E.g. awaiting a database query might spawn a thread or a process and you don't need to care.
Subinterpreters are only relevant if you're writing C-code that embeds the python interpreter. So e.g. the mod_wsgi developers might care. A python developer never uses these directly.
So unless you're a library author or doing fairly low-level things you won't need to care about any of the options but async-await pretty soon. The only awkward thing is that async-await isn't widely used/supported yet leading to there currently being two obvious ways to do it depending on what your libraries support.
tl;dr; Use multiprocessing if you must. use async-await if you can. Let library developer worry about the tricky bits. Pretty soon just use async-await always.
- pansa2 6y ago> Subinterpreters are only relevant if you're writing C-code that embeds the python interpreter. [...] A python developer never uses these directly. The linked article is about making subinterpreters available to pure Python code. > Pretty soon just use async-await always. I can see async/await supplanting threading for I/O-bound concurrency, but it doesn't provide parallelism. For that, maybe subinterpreters will eventually supplant multiprocessing?
- BiteCode_dev 6y agoasync / await is just a protocol to delegate async behavior outside of your control flow. You can provide the async behavior using any solution you want: threads, io event loop, subinterpretters, subprocess... It can all be called from async / await. In fact, if you use asyncio, the lib, it provides mechanisme to await from threads, subprocess.Popen, and multiprocessing pools.