I have a simple python application where I would like to open a connection to the crossbar server, and then publish info messages from time to time.
If I call autobahn.asyncio.component.run(), to open the connection, then the main thread of
control is given to that loop and I cannot run my own functions.
So I would like to essentially have Component() join the crossbar and then be able to get my hands on the session in my main thread and call session.publish whenever I need to.
When I run the code below I get an error. How can I make sure an event loop exists for this,
or else how to manually make a connection to the crossbar server and manually publish to it without requiring the asyncio event loop?
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py", line 602, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.
from autobahn.asyncio.component import Component, run
import threading
# Declaration for purpose of documentation to say these are globals
global this_session
global crossbar
crossbar = Component(
transports=[
{
"type": "websocket",
"url": u"wss://localhost:1964/ws",
"endpoint": {
"type": "tcp",
"host": "localhost",
"port": 1964,
# "tls": context,
},
"options": {
"open_handshake_timeout": 100,
}
},
],
realm=u"leela",
)
@crossbar.on_join
async def join(session, details):
global this_session
print("joined {}".format(details))
this_session = session
#
def publish_something(data):
global this_session
this_session.publish("my.topic.render.json", data)
t1 = threading.Thread(target=run, args=([crossbar],))
t1.start()