如何在外部调用tornado.queues消息

如何在外部调用tornado.queues消息

问题描述:

我用龙卷风来做一个简单的websocket客户端来获得推送,但我不知道如何处理另一个文件中的tornado.queue。 使用打印(que.get()),以获得类似<tornado.concurrent.Future object at 0x106a940b8>如何在外部调用tornado.queues消息

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哪能输出的数据。 py数据?

我刚刚接触不久蟒,如果可能的话,请张贴的全部代码,谢谢:)

tornado.queue.Queue不是线程安全的,并且用于龙卷风应用程序中使用它们一般单线程和事件驱动。你需要做两件事情之一:

  1. 使用旋风无处不在,让b.py使用协同程序和事件,下面就在龙卷风其他地方一样堵代码相同的限制。

    # b.py 
    import a 
    @gen.coroutine 
    def f(): 
        while True: 
         print((yield a.que.get()) 
    
  2. 使用线程安全标准库中queue.Queue。从Tornado写入无限的线程安全队列非常简单(使用put_nowait())。从一读(或写界队列)是棘手的,它往往最容易奉献一个线程任务(除非你有大量的队列):

    # 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)))