Hi, I need interprocess communication through sockets in my panel app. However, it seems that I always lose the connection… Strangely enough, the version where the panel app only reads data seems to work, but not when data is sent… Am I missing something ?

Below are the MWEs:

Server side:

import asyncio
from datetime import datetime

async def buggy_handle_client(reader: asyncio.StreamReader, writer):
    while True:
        line = (await reader.readline()).decode()
        writer.write((str(datetime.now())+"--"+line+"\n").encode())
        await writer.drain()
        await asyncio.sleep(4)

async def not_buggy_handle_client(reader: asyncio.StreamReader, writer):
    while True:
        writer.write((str(datetime.now())+"--"+"\n").encode())
        await writer.drain()
        await asyncio.sleep(4)

async def main():
    #Change to the buggy version here
    s = await asyncio.start_server(buggy_handle_client, host="localhost", port=8887)
    await s.serve_forever()

asyncio.run(main())

Client side:

import panel as pn
from datetime import datetime
import asyncio


to_display = pn.rx("test")

async def working_update_display():
    while True:
        to_display.rx.value = str(datetime.now())
        await asyncio.sleep(1)

async def buggy_update_display(*args, **kwargs):
    reader, writer = await asyncio.open_connection("localhost", 8887)
    while True:
        writer.write("wtest\n".encode())
        await writer.drain()
        to_display.rx.value = (await reader.readline()).decode()
        await asyncio.sleep(1)

async def not_buggy_update_display(*args, **kwargs):
    reader, writer = await asyncio.open_connection("localhost", 8887)
    while True:
        to_display.rx.value = (await reader.readline()).decode()
        await asyncio.sleep(1)

#Change to the buggy version here, needs to match server version
pn.state.onload(buggy_update_display)

pn.pane.Str(to_display).servable()