Re: Asyncio tasks getting cancelled
On Sun, Nov 4, 2018 at 3:58 PM Léo El Amri via Python-list wrote: > > On 04/11/2018 20:25, i...@koeln.ccc.de wrote: > > I'm having trouble with asyncio. Apparently tasks (asyncio.create_task) > > are not kept referenced by asyncio itself, causing the task to be > > cancelled when the creating function finishes (and noone is awaiting the > > corresponding futue). Am I doing something wrong or is this expected > > behavior? > > > > The code sample I tried: > > > >> import asyncio > >> > >> async def foobar(): > >> print(1) > >> await asyncio.sleep(1) > >> print(2) > >> > >> async def main(): > >> asyncio.create_task(foobar()) > >> #await asyncio.sleep(2) > >> > >> loop = asyncio.get_event_loop() > >> asyncio.run(main()) > >> loop.run_forever() > > > > I don't know anything about asyncio in Python 3.7, but given the > documentation, asyncio.run() will start a loop and run the coroutine > into it until there is nothing to do anymore, then free the loop it > created. I assume it's kind of a run_forever() with some code before it > to schedule the coroutine. My understanding of asyncio.run() from https://github.com/python/asyncio/pull/465 is that asyncio.run() is more or less the equivalent of loop.run_until_complete() with finalization, not loop.run_forever(). Thus this result makes sense to me: asyncio.run() runs until main() finishes, then stops. Without the sleep(2), main() starts creates the foobar() task and then returns without awaiting it, so the sleep(1) never finishes. asyncio.run() also finalizes its event loop, so I assume that the loop being run_forever must be a different event loop since running it doesn't just raise an exception. Therefore it doesn't resume the foobar() coroutine since it doesn't know about it. Combining asyncio.run() with loop.run_forever() makes little sense. The purpose of asyncio.run() as I understand it is to avoid having to manage the event loop oneself. If the goal here is for the task created by main() to complete before the loop exits, then main() should await it, and not just create it without awaiting it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Asyncio tasks getting cancelled
On 04/11/2018 20:25, i...@koeln.ccc.de wrote: > I'm having trouble with asyncio. Apparently tasks (asyncio.create_task) > are not kept referenced by asyncio itself, causing the task to be > cancelled when the creating function finishes (and noone is awaiting the > corresponding futue). Am I doing something wrong or is this expected > behavior? > > The code sample I tried: > >> import asyncio >> >> async def foobar(): >> print(1) >> await asyncio.sleep(1) >> print(2) >> >> async def main(): >> asyncio.create_task(foobar()) >> #await asyncio.sleep(2) >> >> loop = asyncio.get_event_loop() >> asyncio.run(main()) >> loop.run_forever() > I don't know anything about asyncio in Python 3.7, but given the documentation, asyncio.run() will start a loop and run the coroutine into it until there is nothing to do anymore, then free the loop it created. I assume it's kind of a run_forever() with some code before it to schedule the coroutine. Thus, I don't think it's appropriate to allocate the loop by yourself with get_event_loop(), then to run it with run_forever(). Usually, when you're already running into a coroutine, you're using "await other_coroutine()" instead of "asyncio.create_task(other_coroutine())". Buy it should not be the root cause of your issue. With theses information only, it looks like a Python bug to me (Or an implementation defined behavior. By the way what is your platform ?). You could try to change either one of asyncio.create_task() or asyncio.run() with "older" Python API (Python 3.6, preferably). In the case of run() it would be a simple loop.create_task(main()) instead of the asyncio.asyncio.run(main()). In the case of asyncio.create_task() it would be asyncio.get_event_loop(foobar()). But anyway, I highly recommend you to use the "await other_coroutine()" syntax I talked about earlier. It may even fix the issue (90% chance). I don't have Python 3.7 right now, so I can't help further. I hope someone with knowledge in asyncio under Python 3.7 will be able to help. - Léo -- https://mail.python.org/mailman/listinfo/python-list
Re: Overwhelmed by the Simplicity of Python. Any Recommendation?
On Sun, Nov 4, 2018 at 11:56 AM Mike C wrote: > > Same here. Debugging in Python is annoying, I like to step through my code > line by line, it's impossible to do it with object-oriented programming > language. > > Also, there's no good REPL IDE. > > Spyder barely works with some basic features. PyCharm, the most popular, > takes too long to start, and you have to setup folders and directories EVERY > SINGLE TIME at startup. I use pdb and I have no issues debugging nor stepping through my code line-by-line when needed. What does it being OO have to do with it? But then again, I also know better then to top post. > Rhodri James wrote: > ... > > I completely agree. I too have come from a background in C, and still > > do most of my day job in C or assembler. It took a while before I was > > writing idiomatic Python, never mind efficient Python (arguably I still > > don't, but as Rob says, who cares?). Don't worry about it; at some > > point you will discover that the "obvious" Python you are writing looks > > a lot like the code you are looking at now and thinking "that's really > > clever, I'll never be able to to that." > > at this stage of my own process in learning, i'm > trying to read the FAQs i can find, any tutorials, > answers to specific questions on stackoverflow on > particular topics to see if i can understand the > issues, etc. > > as for my own code, yes, it's horrible at the > moment, but to me working code is always the > final arbitor. i much prefer simple and stepwise > refinement if speed isn't the issue i think clarity > and simplicity is more important. > > speed is only more important for large projects > that process a ton of data. > > in 3-5yrs i expect to understand more of what > the theory and more conceptual things going on as > i read more of the history and how the language > has developed. > > i won't consider myself fluent until i start > "thinking" in it and can visualise the data > structures/objects in my head and such as i > currently do for C. -- https://mail.python.org/mailman/listinfo/python-list
Asyncio tasks getting cancelled
Hi all, I'm having trouble with asyncio. Apparently tasks (asyncio.create_task) are not kept referenced by asyncio itself, causing the task to be cancelled when the creating function finishes (and noone is awaiting the corresponding futue). Am I doing something wrong or is this expected behavior? The code sample I tried: > import asyncio > > async def foobar(): > print(1) > await asyncio.sleep(1) > print(2) > > async def main(): > asyncio.create_task(foobar()) > #await asyncio.sleep(2) > > loop = asyncio.get_event_loop() > asyncio.run(main()) > loop.run_forever() The sample does print "2" only when uncommenting the asyncio.sleep(2) line. Greetings. -- https://mail.python.org/mailman/listinfo/python-list
Re: Overwhelmed by the Simplicity of Python. Any Recommendation?
Same here. Debugging in Python is annoying, I like to step through my code line by line, it's impossible to do it with object-oriented programming language. Also, there's no good REPL IDE. Spyder barely works with some basic features. PyCharm, the most popular, takes too long to start, and you have to setup folders and directories EVERY SINGLE TIME at startup. From: Python-list on behalf of songbird Sent: Saturday, November 3, 2018 12:45:57 PM To: python-list@python.org Subject: Re: Overwhelmed by the Simplicity of Python. Any Recommendation? Rhodri James wrote: ... > I completely agree. I too have come from a background in C, and still > do most of my day job in C or assembler. It took a while before I was > writing idiomatic Python, never mind efficient Python (arguably I still > don't, but as Rob says, who cares?). Don't worry about it; at some > point you will discover that the "obvious" Python you are writing looks > a lot like the code you are looking at now and thinking "that's really > clever, I'll never be able to to that." at this stage of my own process in learning, i'm trying to read the FAQs i can find, any tutorials, answers to specific questions on stackoverflow on particular topics to see if i can understand the issues, etc. as for my own code, yes, it's horrible at the moment, but to me working code is always the final arbitor. i much prefer simple and stepwise refinement if speed isn't the issue i think clarity and simplicity is more important. speed is only more important for large projects that process a ton of data. in 3-5yrs i expect to understand more of what the theory and more conceptual things going on as i read more of the history and how the language has developed. i won't consider myself fluent until i start "thinking" in it and can visualise the data structures/objects in my head and such as i currently do for C. songbird -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list