Issue
I have done some reading on the topic and found that we can use threading
if some problem is io-bounded but io is not very slow. But I couldn't understand why we still couldn't use asyncio
? Since python threading
is anyway runs on a single thread because of the GIL, what make it more useful than asyncio
?
I.e, in Python is there any problem where threading
is more useful than using asyncio
or multiprocessing
?
I found tons of example of each and differences between each, but still could not find a single source explaining a clear use case of Python threading.
Thank you.
Note: I found this question sounds similar, but I understand why multiprocessing is important when a python process is CPU heavy and can independently run without minimal communication.
Solution
Since you are asking whether it is "replaceable", I'm interpreting the focus of your question as being whether threading can do anything that asyncio cannot.
The reality that Python has a GIL does seriously diminish what would have been threading's greatest strength: its ability to facilitate concurrent processing (of course, Python is starting to finally address this limitation).
Asyncio on its own does not inherently allow for any concurrent processing at all. All it tries to do is provide a clean model for making optimal use of the underlying OS's support for asynchronous IO and of the possibility of doing 'other work' during time that would normally be wasted on blocking IO calls.
Assuming you do have access to an asynchronous version of whatever IO you want to do, then I agree that threading is completely superfluous.
edit, adding more explanation about async libraries
But, if you don't have a suitable async library then threading is still a good option. For operations such as making a web request, maybe you were using http
package. You might just switch to the httpx
package to get access to awaitable versions of get()
, post()
etc. However, if you are relying on a library which interfaces with an API, for example maybe you are using a package that interfaces with twitter, that package may not have functions in it that return an awaitable. When you call get_tweet(id)
or whatever, it may just not return until it has your response. In that case, that is a blocking library and you will not get benefit from async there. So, if you can't find another package for talking to Twitter, then you might be better off using threading (though you still have to ensure that the package you are using is threadsafe... so you're probably still out of luck).
And with upcoming changes to the GIL in Python, threading will probably see a significant increase in usefulness (as people start using it for concurrent processing).
Edit: removing the part showing a threading performance comparison
Answered By - Bill Huneke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.