I'm running a WSS server from a Python script to get certain data from the client. I'd like to receive that data in Blender and use it. I run the process like:
import bpy
from subprocess import Popen, PIPE
import sys
server = Popen([sys.executable, "wss.py"], stdout=PIPE)
def update(scene):
print(server.stdout.readline())
bpy.app.handlers.frame_change_pre.append(update)
Problem is, attempting to access stdout in any way (stdout.read()
or for line in stdout:
) results in Blender waiting for the server process to finish running and freezing. Are there any workarounds for this?
wss.py is as follows, server works fine, but requires an asyncio loop and the process to be running:
import asyncio
import SSL
import websockets
import json
import sys
CONNECTION = None
async def onconnect(websocket, path):
global CONNECTION
CONNECTION = websocket
asyncio.get_event_loop().create_task(messages(websocket))
try:
await websocket.wait_closed()
finally:
CONNECTION = None
async def messages(websocket):
while True:
msg = await websocket.recv()
sys.stdout.write(msg)
sys.stdout.flush()
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain('host.pem', 'host.key')
server = websockets.serve(onconnect, '0.0.0.0', 8000, ssl=ssl_context)
asyncio.get_event_loop().run_until_complete(server)
asyncio.get_event_loop().run_forever()
wss.py
file or if it's something going on in the source code of thestdout.readline()
method. $\endgroup$import pdb; pdb.set_trace()
above where you want to set a breakpoint and then run the program, and you can step through each line of code being executed and see where it's getting stuck. That might give you a bit more insight on what's holding it up. $\endgroup$server.stdout.readline()
until I manually stop the python.exe process, which is running wss.py. I put the breakpoint before the call and stepping once froze pdb too. After stopping the process, pdb didn't reveal anything more than Blender's default drawing methods. $\endgroup$