аватар question@mail.ru · 01.01.1970 03:00

asyncio: the correct stop is not your consignment

I am again with the same problem. In all tank were under my control, and I could manually close them. But the trouble is: I needed webcakes here. In the examples in any correct closure, it is not produced:

  start_server = websockets.Serv (Hello,  'Localhost' ,  8765 ) asyncio.get_event_loop (). Run_Until_complete (StA rt_server) asyncio.get_event_loop (). Run_Forever ()     

I Played with examples, and yes, with Ctrl+C, it swores if there is a connected client:

  $ Python WebSocket_SERVER.PY ^ckeyboardinterrupTexception ignored  in : Task Was Destroyed BUT IT  IS  PENTASK: & LT; TASK Pending Coro = & Lt; Run () Running at websockets/Protocol.py:  235  & gt;  Wait_for = & Lt; Future Pending CB = [Task._wakeup ()] & gt;  cb = [_ wait. & lt;  locals  & gt; ._ on_complement () AT Asyncio/Tasks.py:  399 ] & gt; task was destroyed but it  is  pending! task: & lt; Task pending Coro = & lt; get () running at asyncio/queues.py:  198  & gt;  Wait_for = & Lt; Future Pending CB = [Task._wakeup ()] & gt;  cb = [_ wait. & lt;  locals  & gt; ._ on_complement () AT Asyncio/Tasks.py:  399 ] & gt; task was destroyed but it  is  pending! task: & lt; Task pending Coro = & Lt; Handler () Running at websockets/server.py:  64  & gt;  Wait_for = & Lt; Future Pending CB = [Task._wakeup ()] & gt; & gt;     

since the correct completion in the documentation is not presented and rapidly pumping all kinds server.close () did not lead to any productive result, the question of the correct completion of the application arises again, if the tusks are not controlled by me.

This is just this library of the curve? Or is it typical for any asyncio-bibliotek and I don’t understand something in the very essence of asyncio and someone flows me and some kind of mattress about this?

how to correctly complete the whole case?


with the general case, but websokockets are not to close in no way wants. The minimum example for repetition (based on the @jfs response):

  #!/Usr/bin/env python3   #-*-coding: utf-8-*-   import  asyncio  import  websockts @asyncio.coroutine    def   echo  ws, path ) Class = ""> Print  ( 'Client Started' )   white   true :  data =  yield   from  ws.recv ()   if  data> is   no : : : : : :   break    yield   from  ws.Send (data)   print  ( 'Client Finined' ) loop = asyncio.get_event_loop () start_server = websockets.Serv (echo,  '' 127.0.1 ',  8888 , look = look) server = loop.ru_Until_complete (start_server)   print  ( 'listen' )   try :  server = loop.ru_forever ()   pass  server.close () loop.ru_Until_complete (server.wait_closed ()) loop.close ()   print  ( 'fined' )     

just connect any client to it (at least from the browser & mdash; & nbsp; ws = New WebSocket ('WS: //127.0.1: 8888/'); ) and press Ctrl+C & Mdash; & Nbsp; The previously given error is printed (though without keyboardinterrapt).

аватар answer@mail.ru · 01.01.1970 03:00

asyncio implements cooperative multitasking. This means that websockets the library must cooperate (provide the opportunity to purely complete the compounds). A bug was opened on WebSockets.

, even for ordinary (Preemptive) flows, does not have a general solution that would correctly stop the flows, regardless of the code that they perform (Java lea this lesson in a heavy way). The process of the operating system is safe for stopping the resource if we are not conceed about the loss of data due to an unpeeled file buffer, notification of the other side of the termination of the network connection, the completion of the transaction of the database ITD. If it worries, then the process is also obliged to cooperate, if we want to complete it prematurely.

to process ctrl+c in python, as usual you need to catch keyboardinterept or determine your handle for sigint signal. :

   import  asynco @asyncio.coroutine    def   handle_echo  ( reader, writer ):  data =  yield   from  reader.read ()  message = data.decode ()  addr = Writer.get_extra_info ( 'peeeame' )   print  ( "" reced %r " %(message, Addr))   print  ( "send: % r" " % message)  writer.write (data)   yield   from  writer.dran ()   print  ()  writer.close = loop = loop = loop = loop asyncio.get_event_loop () coro = asyncio.start_server (handle_echo,  '' 127.0.1 ',  8888 , loop = loop) server = loop.ru_undil_complete (Coro)  # SERVE REQUESTS Until Ctrl+C IS Pressed    print  ( 'serving on {}' . class = ""> 0 ]. GetsockName ()))   try :  loop.ru_forver ()   experept  keyboardinterrupt:   pass   # close the server  server.close () loop.ru_until_complete (server.wait_closed ()) loop.close ()      geevent  (Cooperative multitasking) It is also necessary  keyboardinterrtupt  to catch. 

The documentation clearly mentions:

Serve () yields a server Provides a clause () Method and a wait_closed () coroutine to stop serving. The servers can be used exactly the same stop for the correct stop:

   try :  loop.ru_forver ()   Except  keyboardinterrapt:   pass   # class the Server  server.close () loop.ru_undil_complete (server.wait_closed ()) loop.close ()                                       

Latest

Similar