Closed reach4bawer closed 1 year ago
Gevent with asyncio loop running together would cause some problems. I suggest you chose one between them.
I understand. Do you know the reason why it does not play well?
Well, for one thing, asyncio requires cooperative use of the async
keyword. If you call a function that does not use async
/await
it will block. gevent
itself does not support asyncio, therefore is not totally compatible with applications using asyncio, at least perhaps not in the way you'd want them to be.
Anyhow, grequests
and gevent
both predate the implementation of asyncio in Python. So when using grequests
, because none of the internals ever use async
/await
, any use of the functions in grequests
will block everything because it will not give up control of the event loop for other functions to run until the function is done running.
There's likely certain ways to leverage asyncio with this package and make it somewhat useful, but they're probably not very intuitive patterns. The monkey patching that gevent does is also a large source of incompatibility with other libraries, but I don't know what parts, if any, would make it strictly incompatible with asyncio.
However, from a quick search, there do seem to be variations of the gevent lib out there which are async compatible. For example aiogevent
.
I hope that makes sense and helps answer your question @reach4bawer
Thank you so much that makes sense. It does help me understand the reason. I think a work around for this would be to load one library and then remove it from the program memory after it's use. Just so that they might not conflict or become blockers for each other.
I am using grequests to get the data from 15000 hosts that are active. I first ping the hosts to check if they are active or not. That code take around 350 seconds to check the hosts. Once I get them I filter the hosts lists to just the active ones and want to use grequests to get the text from the hosts.
When I ran grequests individually on all the hosts without integrating it with the other code it take around 600 seconds. When I combine the two, I can't get either to work at peak speed for some reason that I cannot figure out.
I am running the following code in the environment with -
My code is as follows -
If I put the code for grequests before I check the status then the async code slows down to crawling speed or gets stuck. Can you point me in the right direction?