Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a async function contained in a class?

Based on this answer I want to build an async websoket client in a class which would be imported from another file:

#!/usr/bin/env python3

import sys, json
import asyncio
from websockets import connect

class EchoWebsocket:
    def __await__(self):
        # see: https://stackoverflow.com/a/33420721/1113207
        return self._async_init().__await__()

    async def _async_init(self):
        self._conn = connect('wss://ws.binaryws.com/websockets/v3')
        self.websocket = await self._conn.__aenter__()
        return self

    async def close(self):
        await self._conn.__aexit__(*sys.exc_info())

    async def send(self, message):
        await self.websocket.send(message)

    async def receive(self):
        return await self.websocket.recv()

class mtest:
    async def start(self):
        try:
            self.wws = await EchoWebsocket()
        finally:
            await self.wws.close()

    async def get_ticks(self):
        await self.wws.send(json.dumps({'ticks_history': 'R_50', 'end': 'latest', 'count': 1}))
        return await self.wws.receive()

if __name__ == '__main__':
    a = mtest()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(a.start())

And I import it in main.py, where I have the following:

from testws import *

a = mtest()
print (a.get_ticks())
print ("this will be printed after the ticks")

But it retrieves me the following error:

root@ubupc1:/home/dinocob# python3 test.py
<coroutine object hello.get_ticks at 0x7f13190a9200>
test.py:42: RuntimeWarning: coroutine 'mtest.get_ticks' was never awaited
  print (a.get_ticks())
this will be printed after the ticks

What's is going on here? Why I'm not able to access mtest.get_ticks if it has the async word at the begining of def?

like image 999
mllamazares Avatar asked Feb 02 '17 17:02

mllamazares


People also ask

Can a class have an async function?

Async Option ConstructorThe async function call can be added right into the class instantiation step, without needing a separate init() call or having to modify your established method of class construction.

How do I use async class?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.

How do you call async method in Python?

To run an async function (coroutine) you have to call it using an Event Loop. Event Loops: You can think of Event Loop as functions to run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Example 1: Event Loop example to run async Function to run a single async function: Python3.

What happens when you call async method without await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.


2 Answers

Finally I could find the right way to do it (special thanks to @dirn)

#!/usr/bin/env python3

import sys, json
import asyncio
from websockets import connect

class EchoWebsocket:
    async def __aenter__(self):
        self._conn = connect('wss://ws.binaryws.com/websockets/v3')
        self.websocket = await self._conn.__aenter__()        
        return self

    async def __aexit__(self, *args, **kwargs):
        await self._conn.__aexit__(*args, **kwargs)

    async def send(self, message):
        await self.websocket.send(message)

    async def receive(self):
        return await self.websocket.recv()

class mtest:
    def __init__(self):
        self.wws = EchoWebsocket()
        self.loop = asyncio.get_event_loop()

    def get_ticks(self):
        return self.loop.run_until_complete(self.__async__get_ticks())

    async def __async__get_ticks(self):
        async with self.wws as echo:
            await echo.send(json.dumps({'ticks_history': 'R_50', 'end': 'latest', 'count': 1}))
            return await echo.receive()

And this in main.py:

from testws import *

a = mtest()

foo = a.get_ticks()
print (foo)

print ("async works like a charm!")

foo = a.get_ticks()
print (foo)

This is the output:

root@ubupc1:/home/dinocob# python3 test.py
{"count": 1, "end": "latest", "ticks_history": "R_50"}
async works like a charm!
{"count": 1, "end": "latest", "ticks_history": "R_50"}

Any tip to improve it is welcomed! ;)

like image 194
mllamazares Avatar answered Oct 12 '22 11:10

mllamazares


Your question and answer are great! They helped me a lot!

Based on your code I was able to create the following class, better matching my need:

import asyncio
from websockets import connect

class TestClient:
    def __init__(self, URL):
        self.URL = URL
        self.conn = None
        self.loop = asyncio.get_event_loop()

    async def send(self, message):
        if self.conn == None:
            self.conn = await connect(self.URL)
        await self.conn.send(message)

    async def receive(self):
        return await self.conn.recv()

    def ping(self):
        return self.loop.run_until_complete(self._ping())

    async def _ping(self):
        await self.send("Hello World")
        return await self.receive()

test = TestClient("wss://echo.websocket.org")
print(test.ping())
like image 6
Ray Hulha Avatar answered Oct 12 '22 10:10

Ray Hulha