I hope websockets can running in background.
Once received the message, print it.
Other function can call it's 'send' function to send message
But the code under 'loop.run_until_complete(Iao())' will no running.
I tried use 'threading' then put websockets in other thread, it's still can't running.
Is it possible to achieve it? except create a async function
import asyncio
import websockets
import time
class WebsocketHandler():
def __init__(self):
self.conn = None
async def connect(self, url):
self.conn = await websockets.connect(url)
async def send(self, msg):
await self.conn.send(msg)
async def receive(self):
while True:
print(await self.conn.recv())
async def close(self):
await self.conn.close()
async def Iao():
global handler
handler = WebsocketHandler()
await handler.connect('ws://localhost:8765')
await handler.receive()
await handler.send('{"action":"plus"}')
await handler.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(Iao())
while True:
print("1")
asyncio.run(handler.send('{"action":"plus"}'))
print("2")
time.sleep(2)
Related
I am trying to call async functions in tornado overriden methods.
Initially, the function generate() is syncronous, but that seems to block the entire program, including on_connection_close method
class Reader:
def __init__(self, topic, group_id):
self.topic = topic
self.consumer = AIOKafkaConsumer(topic, bootstrap_servers="kafka:9092",
group_id=group_id)
async def read(self, sender):
print("enter read...")
#it seems the code below is blocking
await self.consumer.start()
async for message in self.consumer:
sender(json.dumps(consumed_message))
await self.consumer.commit()
class SampleSockeHandler(tornado.websocket.WebSocketHandler):
def on_connection_close(self):
self.app.close()
def on_message(self, message):
generate(message)
#await generate(message)
async def generate(self, message):
await self.reader.read(message)
print("enter read...") is only executed once in the first read() call. In the subsequent read() calls, it does not print anymore.
Now when I comment out all the code below it i.e
await self.consumer.start()
async for message in self.consumer:
sender(json.dumps(consumed_message))
await self.consumer.commit()
it works again
How do I fix this problem? Shouldn't it be an async call?
async def main():
app = Application()
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
asyncio.run(main())
I tested the following code with Ncat. It only sends a single message, then it does not send anything, and does not except. It is also not reading anything.
I have no idea what could be going on. There is no exception, and no data seems to be sent.
import asyncio
loop = asyncio.get_event_loop()
class Client:
def __init__(self):
self.writer = None
self.reader = None
async def connect(self, address, port):
reader, writer = await asyncio.open_connection(address, port)
self.reader = reader
self.writer = writer
print("connected!")
async def send(self, message):
print("writing " + message)
self.writer.write((message + '\n').encode())
await self.writer.drain()
async def receive(self):
print("receiving")
message = (self.reader.readuntil('\n'.encode())).decode()
return message
async def read_loop(self):
while True:
incoming = await self.receive()
print("remote: " + incoming)
async def main():
client = Client()
await client.connect("127.0.0.1", 31416)
loop.create_task(client.read_loop())
while True:
text = input("message: ")
await client.send(text)
loop.run_until_complete(main())
I haven't tested your code but there are things that immediately jump out.
First, you should avoid to use low-level asyncio APIs calling loop.create_task or loop.run_until_complete. Those are intended to be used by frameworks developers. Use asyncio.create_task and asyncio.run instead.
Then, you are getting no exception (or result) because you are not keeping any reference to the task you are creating. You should always keep one to tasks (i.e. assigning it to a variabile or adding it in a list/set).
Last, you are missing an await in the receive method: StreamReader.readuntil is a coroutine function and so you have to await on it.
class Client:
...
async def receive(self):
print('receiving...')
message = await self.reader.readuntil('\n'.encode())
return message.decode()
...
I am trying to run a simple aiohttp server that can be stopped by the user hitting CTRL+C.
However, right now it appears to stop immediately with no error messages after starting. What might be the problem?
import asyncio
from aiohttp import web
class Foo:
def __init__(self):
self.app = web.Application()
self.app.add_routes([])
self.runner = web.AppRunner(self.app)
async def start(self):
await self.runner.setup()
site = web.TCPSite(self.runner, "127.0.0.1", 1234)
await site.start()
async def stop(self):
await self.runner.cleanup()
async def main():
await foo.start()
if __name__ == '__main__':
foo = Foo()
try:
asyncio.run(main())
except KeyboardInterrupt:
asyncio.run(foo.stop())
I have this basic exchange monitor script. I'm trying to create one thread per symbol, apart from the main thread which is handling other work, and have them listen to public Gemini websocket endpoints. I'm getting the first thread running and printing exchange data to the console, but not the second one. I had expected to see data from both threads being printed at approximately the same time. I've tried using the threading library instead of asyncio and encountered the same situation.
I realize my two public API MarketWebsocket classes could be combined to be cleaner, I'm still trying to work out a way to easily add other symbols to the list. Thanks for any nudges in the right direction!
import asyncio
from websockets import connect
symbols_to_watch = [
"BTCUSD",
"ETHUSD"
]
class BTCMarketWebsocket:
disable = False
async def __aenter__(self):
symbol = symbols_to_watch[0]
self._conn = connect("wss://api.gemini.com/v1/marketdata/{}".format(symbol))
self.websocket = await self._conn.__aenter__()
return self
async def __aexit__(self, *args, **kwargs):
await self._conn.__aexit__(*args, **kwargs)
async def receive(self):
return await self.websocket.recv()
class ETHMarketWebsocket:
disable = False
async def __aenter__(self):
symbol = symbols_to_watch[1]
self._conn = connect("wss://api.gemini.com/v1/marketdata/{}".format(symbol))
self.websocket = await self._conn.__aenter__()
return self
async def __aexit__(self, *args, **kwargs):
await self._conn.__aexit__(*args, **kwargs)
async def receive(self):
return await self.websocket.recv()
async def btcMarketWebsocket():
async with BTCMarketWebsocket() as btcMarketWebsocket:
while not btcMarketWebsocket.disable:
print(await btcMarketWebsocket.receive())
async def ethMarketWebsocket():
async with ETHMarketWebsocket() as ethMarketWebsocket:
while not ethMarketWebsocket.disable:
print(await ethMarketWebsocket.receive())
if __name__ == '__main__':
asyncio.run(btcMarketWebsocket())
asyncio.run(ethMarketWebsocket())
You can do
async def multiple_tasks():
Tasks =[]
Tasks.append(btcMarketWebsocket())
Tasks.append(ethMarketWebsocket())
await asyncio.gather(*Tasks, return_exceptions=True)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(multiple_tasks())
I am currently developing a server program in Python that uses the websockets and asyncio packages.
I got a basic script handling websockets working (Exhibit A). This script locks when waiting for input, which is not what I want.
The solution for this that I imagine is I can start two asynchronous tasks - one that handles inputs and one that handles outputs - and start them in a secondary event loop. I had to do some research about coroutines, and I came up with Exhibit B as a proof of concept for running two things simultaneously in an event loop.
Now what I'm stuck on is Exhibit C. When I attempted to use this in a practical scenario with the websockets package, I found that websocket.recv() never finishes (or the coroutine never un-pauses - I'm not sure what's going on exactly). In exhibit A it works fine, and I've determined that the coroutine definitely runs at least up until that point.
Any ideas?
Exhibit A:
#!/usr/bin/python3
import asyncio
import websockets
import time
# This works great!
async def hello(websocket, path):
while True:
# This line waits for input from socket
name = await websocket.recv()
print("< {}".format(name))
# "echo... echo... echo... echo... echo..."
greeting = ''.join(name + "... " for x in range(5))
await websocket.send(greeting)
print("> {}".format(greeting))
time.sleep(0.1);
start_server = websockets.serve(hello, '', 26231)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Exhibit B:
#!/usr/bin/python3
import asyncio
import time
class Thing:
def __init__(self):
pass
async def test(self):
for x in range(10):
print("Hello"+str(x))
await asyncio.sleep(0)
def run(self):
# Add the task to the event loop twice
asyncio.ensure_future(self.test())
asyncio.ensure_future(self.test())
t = Thing()
t.run()
loop = asyncio.get_event_loop();
loop.run_forever()
Exhibit C:
#!/usr/bin/python3
import asyncio
import websockets
import time
class WebsocketRequest:
def __init__(self, websocket):
self.websocket = websocket
# Works great
async def handle_oputs(self):
# This works fine - sends a message
# every 10 seconds to the client
while True:
print("sending...")
await self.websocket.send("Hello")
print("> {}".format("Hello"))
time.sleep(10)
# Doesn't work
async def handle_iputs(self):
# This stops at the await and never receives
# any input from the client :/
while True:
try:
print("receiving...")
# This is the line that causes sadness
data = await self.websocket.recv()
print("< {}".format(data))
except:
# This doesn't happen either
print("Listener is dead")
async def run(self):
# This is the part where the coroutine for
# a client get split off into two of them
# to handle input and output separately.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncio.ensure_future(self.handle_iputs())
asyncio.ensure_future(self.handle_oputs())
loop.run_forever()
class WebsocketServer:
def __init__(self, address):
self.ip = address[0]
self.port = address[1]
async def hello(self, websocket, path):
req = WebsocketRequest(websocket)
await req.run()
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
start_server = websockets.serve(self.hello, self.ip, self.port)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Maybe the module websocket (unlike websockets) can help you.
The use of WebsocketApp is very simple:
from websocket import WebSocketApp
class ExampleClass(object):
def __init__(self):
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp("ws://echo.websocket.org",
on_message=on_message,
on_error=on_error,
on_close=on_close)
def on_message(ws, msg):
print "Message Arrived:" + msg
def on_error(ws, error):
print error
def on_close(ws):
print "Connection Closed"
def on_open(ws):
ws.send("Hello!")
To download this module: https://pypi.python.org/pypi/websocket-client
time.sleep() is a blocking operation, so any other tasks cannot interrupt and are not scheduled. Use await asyncio.sleep() instead.