I have got a problem with Autobahn under Python which makes me become desperate and I hope I will find help here.
I’m supposed to write a Python test program which tests the websocketconnection of a mainprogram to make sure that the desired functionality is accurate.
I got a test framework on hand which opens the main program anew for each single test so that I always got the same test environment. However this means that I have to open the connection anew before each test.
Then I would need a blocking receive with a timeout so I could say „Now something is supposed to come“.
For the websocket connection I am supposed to use Autobahn with asyncio.
My tries yet:
To make asyncio work you need to somehow make the event-loop run. Thats why I thought I’d let all test run in a subroutine and await it with run-until-complete so that I subsequently can stop the test program. Inside of the loop I afterwards open a new connection for each test.
I can’t use the code of the examples anymore:
coro = loop.create_connection(factory, ‘127.0.0.1’, 9000)
A run_until_complete musn’t be called inbetween an event loop. The consideration „ the program is already in the loop anyway so I don’t need an extra call anymore“ isn’t productive either. Because there is a lot to do in the tests Autobahn has to wait his turn pretty long. That’s why as soon as there’s the time I get an error message that the handshake didn’t work out.
I tried in several different ways now but nothing would work out. But that might be because I haven’t seen through asyncio completely yet.
My second idea: I know threads! I let Autobahn run in a thread, the queue is threadsafe anyway, so the communication is clear.
from autobahn.asyncio.websocket import WebSocketClientProtocol,
from queue import Queue
from _thread import start_new_thread
self.factory.client = self
def onMessage(self, payload, isBinary):
def onClose(self, wasClean, code, reason):
self.factory.client = None
def init(self, uri, queue, loop):
self.client = None
self.queue = queue
WebSocketClientFactory.__init__(self, uri, loop=loop, debug=False)
self.queue = Queue()
self.loop = asyncio.get_event_loop()
self.factory = MockFactory("ws://127.0.0.1:9000“, self.queue, self.loop)
self.factory.protocol = MyClientProtocol
coro = self.loop.create_connection(self.factory, ‘127.0.0.1’, 9000)
if self.factory.client is not None:
self.factory.client = None
with self.queue.mutex: self.queue.queue.clear()
def receive(self, timeout=2):
return self.queue.get(True, timeout)
This is the latest version of my numerous tries. For example with new_event_loop in the thread ( get_event_loop doesn’t work because I always get the error message „There is no current event loop in thread 'Dummy-2‘.“)
Or I also tried to not build up the thread in connect each time but to do it once in init. The method websocket_thread remained as in the example, just the carrying out of create_connection was done in the method connect. But this didn’t work out.
The current version doesn’t work either, but gives the following error message:
File “/usr/lib/python3.3/site-packages/autobahn/asyncio/websocket.py”, line 54, in connection_made
File “/usr/lib/python3.3/site-packages/autobahn/asyncio/websocket.py”, line 72, in _consume
self.waiter = Future()
File “/usr/lib/python3.3/site-packages/asyncio/futures.py”, line 148, in init
self._loop = events.get_event_loop()
File “/usr/lib/python3.3/site-packages/asyncio/events.py”, line 465, in get_event_loop
File “/usr/lib/python3.3/site-packages/asyncio/events.py”, line 413, in get_event_loop
AssertionError: There is no current event loop in thread 'Dummy-2‘.
I always give the loop to the factory-constructor but internally a feature without the loop gets opened. Thats is why the feature tries again to get the loop by executing the function get_event_loop.
That you very much for your support.