@@ -379,7 +379,7 @@ def _close(self, exc=None):
379
379
self ._remove_reader ()
380
380
if self ._flushed ():
381
381
self ._remove_writer ()
382
- self ._loop .call_soon (self ._call_connection_lost , exc )
382
+ self ._loop .create_task (self ._call_connection_lost ( exc ) )
383
383
384
384
def _abort (self , exc ):
385
385
"""Close the transport immediately.
@@ -393,9 +393,9 @@ def _abort(self, exc):
393
393
self ._closing = True
394
394
self ._remove_reader ()
395
395
self ._remove_writer () # Pending buffered data will not be written
396
- self ._loop .call_soon (self ._call_connection_lost , exc )
396
+ self ._loop .create_task (self ._call_connection_lost ( exc ) )
397
397
398
- def _call_connection_lost (self , exc ):
398
+ async def _call_connection_lost (self , exc ):
399
399
"""Close the connection.
400
400
401
401
Informs the protocol through connection_lost() and clears
@@ -405,16 +405,17 @@ def _call_connection_lost(self, exc):
405
405
assert not self ._has_writer
406
406
assert not self ._has_reader
407
407
try :
408
- self ._serial .flush ( )
408
+ await self ._loop . run_in_executor ( None , self . _serial .flush )
409
409
except (serial .SerialException if os .name == "nt" else termios .error ):
410
410
# ignore serial errors which may happen if the serial device was
411
411
# hot-unplugged.
412
412
pass
413
+
413
414
try :
414
415
self ._protocol .connection_lost (exc )
415
416
finally :
416
417
self ._write_buffer .clear ()
417
- self ._serial .close ( )
418
+ await self ._loop . run_in_executor ( None , self . _serial .close )
418
419
self ._serial = None
419
420
self ._protocol = None
420
421
self ._loop = None
@@ -445,7 +446,7 @@ async def create_serial_connection(loop, protocol_factory, *args, **kwargs):
445
446
446
447
Any additional arguments will be forwarded to the Serial constructor.
447
448
"""
448
- serial_instance = serial .serial_for_url ( * args , ** kwargs )
449
+ serial_instance = await loop . run_in_executor ( None , serial .serial_for_url , * args , ** kwargs )
449
450
transport , protocol = await connection_for_serial (loop , protocol_factory , serial_instance )
450
451
return transport , protocol
451
452
0 commit comments