question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an option to run an asyncio event loop automatically

See original GitHub issue

Experimenting with asyncio righ now is a pain. If I want just to use aiohttp to make a get request, I need to do:

import asyncio
import aiohttp

async def main():
    content = await asyncio.get('http://foo.com').content
    print(content)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Compare to doing the same with requests:

import requets
print(requests.get('http://foo.com').content)

I wish ptpython would have an --asyncio option which starts an event loop with the shell and run any command in it.

So my example with aiohttp would become:

$ ptpython --asyncio

Then:

import aiohttp
print(await asyncio.get('http://foo.com').content)

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sametmaxcommented, Mar 7, 2016

I talked with an iPython dev, he provided me with a %magic_command which open a bloc that does just that. Since ptpython doesn’t have this concept, wrapping could work, but then you need to give it access to the variable of the previous scope, which mean you need to copy local(), pass it to the functions, and merge it to the new locals() dict.

0reactions
Congeecommented, Dec 21, 2019

wait. does https://bugs.python.org/issue37028 mean we can use top level await in ptpython with python 3.8 without any change?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add an option to run an asyncio event loop automatically #9166
I've whipped together a quick extension that adds an %%asyncio cell magic. You can use it like this: In [1]: %load_ext asyncio_magic In...
Read more >
Event Loop — Python 3.11.1 documentation
The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run...
Read more >
asyncio create_task to run forever - python - Stack Overflow
1 Answer 1 · loop.run_until_complete(some_func()) - what you already used; run the event loop until the some_func coroutine finishes. · loop.
Read more >
Asyncio Event Loops Tutorial - TutorialEdge.net
In this tutorial we look at the various ways you can define and work with event loops in Asyncio.
Read more >
How to Create Asyncio Tasks in Python
A task is executed independently. This means it is scheduled in the asyncio event loop and will execute regardless of what else happens...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found