How do I run an method async inside a class that is being used with asyncio.create_server?
<p>I'm using autobahn with asyncio and I'm trying to make a method inside a class that extends WebSocketServerFactory run every x seconds.</p>

<p>This is how the documentation on the autobahn website does it:</p>

<pre><code>from autobahn.asyncio.websocket import WebSocketServerFactory
factory = WebSocketServerFactory()
factory.protocol = MyServerProtocol

loop = asyncio.get_event_loop()
coro = loop.create_server(factory, '127.0.0.1', 9000)
server = loop.run_until_complete(coro)
</code></pre>

<p>I have simply replaced the WebSocketServerFactory class with a class that extends it, and I want to run a method inside it every x seconds.</p>

<p>I found an example of this on the autobahn website, but it uses twisted and not asyncio.</p>

<p>Here is an short example(<a href="https://github.com/crossbario/autobahn-python/blob/master/examples/twisted/websocket/broadcast/server.py" rel="nofollow">original and full version</a>) of what I want, but the example uses twisted:</p>

<pre><code>class CustomServerFactory(WebSocketServerFactory):

def __init__(self, url):
    WebSocketServerFactory.__init__(self, url)
    self.tick()

def tick(self):
    print("tick!")
    self.do_something()
    reactor.callLater(1, self.tick)
</code></pre>

<p>How can I achive the same thing using asyncio?</p>

<p>Based on your example, the same functionality with asyncio can be done using the asyncio event loop: <a href="https://docs.python.org/3/library/asyncio-eventloop.html#delayed-calls" rel="nofollow">Asyncio delayed calls</a>.</p>

<p>So in your example that would mean something like this:</p>

<pre><code>def tick(self):
    print("tick!")
    self.do_something()
    loop.call_later(1, self.tick)
</code></pre>

<p>where loop is the asyncio event loop variable you create earlier in:</p>

<pre><code>loop = asyncio.get_event_loop()
</code></pre>

python python-asyncio autobahn