Package: aiosock
aiosock -- Multiprocess communication sockets for asyncioThis package wraps thesocketlib, so that it can be used as part of the non-blocking asyncio event loop. A goal scenario is multiprocess communication with the help of  coroutines,i.e., combiningmultiprocessingandasyncio.For example, there are two processes in a program: one isMain_Process, and the other isIO_Process. The two processes are running with respective event loops. TheMain_Processis asynchronizedly reading messages fromIO_Process, and once theIO_Processsends something to theMain_Process, the latter will call acallbackfunction or continue executing a coroutine from the previous break point.Quick StartFirst, installaiosock.pip install aiosockSecond, import modulesimportaiosockimportasyncioThird, create aio-socket pairsock1,sock2=aiosocket.aiosockpair()Forth, if you wish to use a callback function, initialize the aio-socket(s) with the function. The form of the function should bedefcallback(obj_recv:Any,*args:Any):...The first argument is the object received from the other socket, and the remainder are arguments passed from where the socket is initialized. For exampledefcallback_print(obj_recv:Any,num:int):print(f'{num}:{obj_recv}')sock1.init((callback_print,0))Fifth, you may also useawaitto read something in anasyncfunction, rather than using a callback. For exampleasyncdefmain():obj_recv=awaitsock1.read()Sixth, you are able to set callbacks for both read and write. the read callback is executed whenever something is received, and the write callback is executed when it is available to write. For exampledefcallback_print(obj_recv:Any,num:int):print(f'{num}:{obj_recv}')defcallback_writable():print('Now writable.')sock1.init((callback_print,0),callback_writable)The defaut values of the callbacks areNone.Seventh, send any object you want, as long as the object is serializable.sock2.send('Hello asyncio!')You can callsock2.send(...)in another process and initialized thesock2in that process, so that multiple processes are able to communicate with each other.Finally, don't forget to run the event loop, for exampleasyncio.get_event_loop().run_forever()Here is a complete demonstration below.DemoCode:frommultiprocessingimportProcessimportosfromtypingimportAny,Iterable,MappingfromaiosockimportAioSock,aiosockpairimportasyncioclassIO_Process(Process):''''''def__init__(self,sock:AioSock,group=None,name:'str | None'=None,args:Iterable[Any]=(),kwargs:Mapping[str,Any]={},*,daemon:'bool | None'=None)->None:super().__init__(group,None,name,daemon=daemon)self.sock=sockdefrun(self):print(f'IO Process PID:{os.getpid()}')self.loop=asyncio.get_event_loop()self.sock.init(self.on_read)self.loop.call_later(3,self.sock.write,f'[sock2 write] PID:{os.getpid()}')self.loop.create_task(self.read())self.loop.run_forever()defon_read(self,obj:Any,*args):''''''print(obj)print(f'[io on_read] PID:{os.getpid()}')asyncdefread(self):''''''whileTrue:obj=awaitself.sock.read()print(f'[io await read]{str(obj)}')print(f'[io await read] PID:{os.getpid()}')asyncdefmain(sock:AioSock):''''''sock1.write(f'[sock1 write] PID:{os.getpid()}')obj=awaitsock.read()print(obj)print(f'[main await read] PID:{os.getpid()}')if__name__=='__main__':print('IO Process Read/Write, Main Process Write/Read.')print(f'Main Process PID:{os.getpid()}')sock1,sock2=aiosockpair()iop=IO_Process(sock2)sock1.init()iop.start()loop=asyncio.get_event_loop()loop.run_until_complete(main(sock1))Output:Main Process PID: 15408
IO Process PID: 12324
[sock1 write] PID: 15408
[io on_read] PID: 12324
[io await read][sock1 write] PID: 15408
[io await read] PID: 12324
[sock2 write] PID: 12324
[main await read] PID: 15408