Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit after Run #6

Open
ltieman opened this issue Sep 19, 2017 · 3 comments
Open

Exit after Run #6

ltieman opened this issue Sep 19, 2017 · 3 comments

Comments

@ltieman
Copy link

ltieman commented Sep 19, 2017

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`
@tankorsmash
Copy link

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.

@anentropic
Copy link

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

@nathanjr
Copy link

nathanjr commented Jun 22, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants