Home > other >  How do this code to the asynchronous?
How do this code to the asynchronous?

Time:10-01

Test and turned out to be simultaneous execution of the following code, how do I change to asynchronous? Thank you very much!

 
The import tornado. Ioloop
The import tornado. Web
The import asyncio

LST=[I for I in range (50)]


Async def diaodu_thread () :
For I in LST:
Await walk (I)


Async def walk (arg) :
Print (' -- -- start % s' % arg)
Await asyncio. Sleep (3)
Print (" -- - end % s' % arg)


The class MainHandler (tornado. Web. RequestHandler) :
Def get (self) :
The self. The write (" Hello, world ")


Def make_app () :
Return tornado. Web. Application ([
(MainHandler, r "/"),
])


If __name__=="__main__" :
Asyncio. Run (diaodu_thread ())
App=make_app ()
App. Listen (8888)
Tornado. Ioloop. Ioloop. Current (). The start ()

CodePudding user response:

Diaodu_thread function to modify the
 
Async def diaodu_thread () :
# for the I in LST:
# await walk (I)
The tasks=[]
For I in LST:
The tasks. Append (asyncio. Create_task (walk) (I))
The tasks await asyncio. Gather (*)

CodePudding user response:

reference 1/f, ice wind of reply:
diaodu_thread function do modify
 
Async def diaodu_thread () :
# for the I in LST:
# await walk (I)
The tasks=[]
For I in LST:
The tasks. Append (asyncio. Create_task (walk) (I))
The tasks await asyncio. Gather (*)


Test once, if instead of this, it's ok to:
 
Async def diaodu_thread () :
Coroutines=[]
For I in LST:
Coroutines. Append (walk) (I)
Await asyncio. Wait (coroutines)
  • Related