I used tornado to do a simple websocket client to get the push, but I do not know how to handle tornado.queues in another file.
Using print (que.get ()) to get the data similar to <tornado.concurrent.Future object at 0x106a940b8>
a.py
from tornado.ioloop import IOLoop, PeriodicCallback
from tornado import gen
from tornado.websocket import websocket_connect
from tornado.queues import Queue
que = Queue()
class Client(object):
def __init__(self):
self.ioloop = IOLoop.instance()
self.connect()
self.ioloop.start()
@gen.coroutine
def connect(self):
ws = yield websocket_connect('ws://127.0.0.1:8001/')
while True:
msg = yield ws.read_message()
que.put(msg)
print(que.get())
if __name__ == '__main__':
Client()
b.py
import a
awe = a.que
while True:
print(awe.get())
b.py how can I output a.py data?
I just touch python soon, if possible, please post the full code, thanks :)
tornado.queue.Queue
is not thread-safe and is intended for use within Tornado applications which are generally single-threaded and event-driven. You need to do one of two things:
Use Tornado everywhere and make b.py
use coroutines and events, following the same restrictions on blocking code as elsewhere in Tornado.
# b.py
import a
@gen.coroutine
def f():
while True:
print((yield a.que.get())
Use the thread-safe queue.Queue
from the standard library. Writing to an unbounded thread-safe queue from Tornado is easy (use put_nowait()
). Reading from one (or writing to a bounded queue) is trickier and it's often easiest to dedicate a thread to the task (unless you have a large number of queues):
# a.py
que = queue.Queue()
executor = concurrent.futures.ThreadPoolExecutor()
@gen.coroutine
def connect(self):
ws = yield websocket_connect(...)
while True:
msg = yield ws.read_message()
que.put_nowait(msg)
print((yield executor.submit(que.get)))