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

Exit after Run

Open ltieman opened this issue 8 years ago • 3 comments
trafficstars

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`

ltieman avatar Sep 19 '17 00:09 ltieman

I'm having the same problem, probably for the same reasons of not really understanding, but calling session.run exits the process no matter what. Seems like you'd need to run everything inside that session.run function if you want to do something with it later.

tankorsmash avatar Oct 09 '17 20:10 tankorsmash

Same here

it comes from the underlying Twisted code

python3.6/site-packages/twisted/internet/task.py in react(main, argv, _reactor)
    935     finished.addBoth(cbFinish)
    936     _reactor.run()
--> 937     sys.exit(codes[0])

is there no way to use this in a project other than having a main function that sys exits when finished?

I guess this lib is specialised for Twisted apps where this weird behaviour must be normal or something

anentropic avatar Nov 14 '17 22:11 anentropic

Hi,

I was having an issue with this also and like @anentropic found that this is expected behaviour for twisted.internet.task.react on completion. https://twistedmatrix.com/documents/15.0.0/api/twisted.internet.task.react.html

You can work around this by simply handling the SystemExit exception.

eg.

    def do_requests(self):
        try:
            self.session.run(self._main)
        except SystemExit:
            logger.info("Ignoring twisted.internet.task.react sys.exit on completion.")

This could probably be better handled within requests-threads, rather than expecting it to be handled by the user.

I'll take a closer look later on and perhaps submit a pull request.

Cheers,

Nathan.

nathanjr avatar Jun 22 '18 06:06 nathanjr