36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
#Inter proccess Communication server, mostly vide coded
|
|
import asyncio
|
|
import pickle, struct
|
|
|
|
async def start_serving(queue: asyncio.Queue, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
|
try:
|
|
|
|
while True:
|
|
try:
|
|
header = await reader.readexactly(4)
|
|
except asyncio.IncompleteReadError:
|
|
break # connection closed cleanly, header wasn't sent — exit loop
|
|
|
|
(length,) = struct.unpack(">I", header)
|
|
|
|
try:
|
|
data = await reader.readexactly(length)
|
|
except asyncio.IncompleteReadError:
|
|
break # connection closed mid-message — exit loop
|
|
|
|
try:
|
|
obj = pickle.loads(data)
|
|
except (pickle.UnpicklingError, AttributeError, ModuleNotFoundError, ImportError, EOFError) as e:
|
|
print(f"Skipping unpicklable message: {e}")
|
|
continue # stream position still valid, safe to read next message
|
|
|
|
queue.put_nowait( obj )
|
|
|
|
except ConnectionResetError: pass
|
|
finally:
|
|
try:
|
|
writer.close()
|
|
await writer.wait_closed()
|
|
except (ConnectionResetError, BrokenPipeError, OSError):
|
|
pass
|