2017-10-20 73 views
0

我用龍捲風來做一個簡單的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數據?

我剛剛接觸不久蟒,如果可能的話,請張貼的全部代碼,謝謝:)

回答

0

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