requests / requests-threads Goto Github PK
View Code? Open in Web Editor NEW๐ญ Twisted Deferred Thread backend for Requests.
๐ญ Twisted Deferred Thread backend for Requests.
The session.run()
function has no path for a non-coroutine function being passed in, it'll simply return without calling twisted.react()
:
requests-threads/requests_threads.py
Lines 61 to 72 in b4d4384
Under Python 3, line 63 is True
, but if line 65 is False
(so f
is not a coroutine function) , the function ends. Should the else
on line 70 be dropped, and lines 71 and 72 unindented?
The example in the readme file reads:
from requests_threads import AsyncSession
session = AsyncSession(n=100)
async def _main():
rs = []
for _ in range(100):
rs.append(await session.get('http://httpbin.org/get'))
print(rs)
if __name__ == '__main__':
session.run(_main)
shouldn't it be
from requests_threads import AsyncSession
session = AsyncSession(n=100)
async def _main():
rs = []
for _ in range(100):
rs.append(session.get('http://httpbin.org/get'))
for i in range(100):
rs[i] = await rs[i]
print(rs)
if __name__ == '__main__':
session.run(_main)
to be run async?
I get this error:
Traceback (most recent call last):
File "C:\Users\Menna\Desktop\requests-futures\twayback.py", line 3, in <module>
session = AsyncSession(n=100)
File "C:\Users\Menna\AppData\Local\Programs\Python\Python310\lib\site-packages\requests_threads.py", line 37, in __init__
asyncioreactor.install(loop)
File "C:\Users\Menna\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\asyncioreactor.py", line 308, in install
reactor = AsyncioSelectorReactor(eventloop)
File "C:\Users\Menna\AppData\Local\Programs\Python\Python310\lib\site-packages\twisted\internet\asyncioreactor.py", line 63, in __init__
raise TypeError(
TypeError: ProactorEventLoop is not supported, got: <ProactorEventLoop running=False closed=False debug=False>
upon running the following Python script:
https://pastebin.com/raw/xrujJmSu
Use the following example command if you'd like to run the script:
script.py -u jack -from 20220205 -to 20220206
Hello,
I'm just learning async, so this might just be dumb, and it seems my piece here is working fine, other than it is returning Process finished with exit code 0
after I run the class.
`class foo:
def __init__(self):
self.session=AsyncSession(n=100)
self.main_out=self.session.run(self.main)
print("still alive")
async def main(self):
rs = []
for _ in range(100):
rs.append(await self.sub('bar'))
return[ (x[0].json(),x[1]) for x in rs]
async def sub(self,key):
return await self.session.get('http://httpbin.org/get'),key`
I'm not that familiar with this new AsyncSession object, but it looks to me like the async/await example doesn't actually perform those GET requests concurrently as you are awaiting for each one to return one by one. Should that example be this instead?
async def _main():
for future in asyncio.as_completed([session.get('http://httpbin.org/get') for _ in range(100)]):
print(await future)
Can we use the post method here?
I am a newbie for using this library.
I want to call an sub-function in main() to do http_get multiple urls, and return all responses by dict().
But I don't how to use this python 2.7 sample code to finish it.
I try to move code from main() to other sub-function and use returnValue(dict()) to return to main(), but the program exits when inlineCallbacks sub-function is done, so main function can not do anything after calling sub-function.
Could anyone can give some idea or direction? Thanks a lot.
from twisted.internet.defer import inlineCallbacks
from twisted.internet.task import react
from requests_threads import AsyncSession
session = AsyncSession(n=100)
@inlineCallbacks
def main(reactor):
responses = []
for i in range(100):
responses.append(session.get('http://httpbin.org/get'))
for response in responses:
r = yield response
print(r)
if __name__ == '__main__':
react(main)
Currently the documentation provides an example on for GET request. Can an example for POST request that demonstrates sending a payload and setting headers be add to the documentation?
Would it be something like this
from requests_threads import AsyncSession
session = AsyncSession(n=100)
async def _main():
url = u"https://api.github.com/repos/psf/requests/issues/482/comments"
body = json.dumps({u"body": u"Sounds great! I'll get right on it!"})
response = await session.post(url=url, data=body)
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.