Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Async tasks on startup (asyncio newbie) #1288

Unanswered
pihentagy asked this question in Q&A
Discussion options

Hi!

I'm new to asyncio in python, and need a bit of help how to add async things.
I'd like to have an async initialization method (for an autocomplete), but I don't want to block the user and let them use the app while querying. Not even starting is ever printed...

import asyncio 
import cmd2 
 
class MyCmdApp(cmd2.Cmd): 
 def __init__(self, *args, **kwargs): 
 super().__init__(*args, **kwargs) 
 self.loop = asyncio.get_event_loop() 
 self.result = 'not yet' 
 self.task = self.loop.create_task(self.long_running_task()) 
 print(self.task) 
 
 async def long_running_task(self): 
 # Simulate a long-running task 
 self.poutput('Starting') 
 await asyncio.sleep(5) 
 self.result = "Done!" 
 print("done") 
 def do_pr(self, _): 
 print(self.result) 
 
if __name__ == '__main__': 
 app = MyCmdApp() 
 app.cmdloop() 
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /