requests-threads icon indicating copy to clipboard operation
requests-threads copied to clipboard

Do multiple http_get url and get all responses, then return all responses to main()

Open monexiao opened this issue 7 years ago • 0 comments

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)

monexiao avatar Mar 27 '18 14:03 monexiao