-1
$\begingroup$

I'm trying to implement a tcp server inside blender with a time operator using asyncio. but the code always crash the UI. I know there is a way to do this inside blender since some people have achieved it already...i found that blender cloud addon..can be the way to go but i don't know where to start any help???

This is the code im trying to mix with the blender cloud addon.. i think i need to integrate the async_loop.py file but i don't know how to start

import asyncio

async def handle_echo(reader, writer):
   data = await reader.read(100)
   message = data.decode()
   addr = writer.get_extra_info('peername')
   print("Received %r from %r" % (message, addr))

   print("Send: %r" % message)
   writer.write(data)
   await writer.drain()

   print("Close the client socket")
   writer.close()

loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
server = loop.run_until_complete(coro)

# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
   loop.run_forever()
except KeyboardInterrupt:
   pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

right now the code im using is by creating a file and reading it with a time operator like it is described here

send instructions to blender from external application

$\endgroup$
  • 2
    $\begingroup$ Since you don't post any code nor details about the crash, there is little we can tell about your situation. You could take a look at how I integrated asyncio with the Blender Cloud add-on. $\endgroup$ – dr. Sybren Jul 7 '17 at 23:47
  • $\begingroup$ Hi, @dr.Sybren, sorry about that... i found the blender cloud add-on but im still relatively new to python so im trying to understand the code....i updated my question so you can understand better my issue $\endgroup$ – Ymmanuel Jul 8 '17 at 3:32
1
$\begingroup$

loop.run_forever() will make your script block Blender itself. You have to call loop.stop() before that, so that run_forever() will only do a single iteration over the asyncio tasks that can be run at that time. Calling this single iteration then has to be performed frequently. This is what the asyncio.loop modal operator is for.

This is quite a complex thing to attempt when you're relatively new to Python. Probably it would be easier to just use multi-processing and start your server in a separate process, instead of using asyncio.

$\endgroup$
  • $\begingroup$ mm, i understand is complex...still a separate server is not that different from what i have now....and is not what i'm looking for...i understand the theory of the loop run forever and loop stop...just need more guidance on how asyncio.loop works.... thanks anyway $\endgroup$ – Ymmanuel Jul 8 '17 at 13:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.