2014-02-16 5 views
0

send() 외부 이벤트를 기반으로 websocket 명령을 트리거 할 수 있습니까? 나는 데이터베이스가 갱신 될 때마다 클라이언트에게 푸시하려고 노력하고있다. 나는 등 기본 코드는 SQL notify하는 uwsgi 파일 모니터 장식을 사용 해봤이 출력에 "DB 변경"인쇄하지만 클라이언트가 '변경 DB'메시지를받을 수 없습니다Flask의 외부 이벤트에서 WebSocket 트리거

from flask.ext.uwsgi_websocket import GeventWebSocket 
from uwsgidecorators import * 

ws = GeventWebSocket(app) 

@ws.route('/feed') 
def socket(ws): 
    ws_readystate = ws.receive() 
    if ws_readystate == '1': 
     ws.send(json.dumps('this message is received just fine')) 
     # client is ready, start something to trigger sending a message here 
     @filemon("../mydb.sqlite") 
     def db_changed(x): 
      print 'DB changed' 
      ws.send(json.dumps('db changed')) 

입니다.

uwsgi --master --http :5000 --http-websockets --gevent 2 --wsgi my_app_name:app 

답변

0

gevent 큐는 패턴

이것은 당신이 계획이 도트 경우 상황

from uwsgidecorators import * 
from gevent.queue import Queue 

channels = [] 

@filemon('/tmp',target='workers') 
def trigger_event(signum): 
    for channel in channels: 
     try: 
      channel.put_nowait(True) 
     except: 
      pass 

def application(e, sr): 
    sr('200 OK', [('Content-Type','text/html')]) 
    yield "Hello and wait..." 
    q = Queue() 
    channels.append(q) 
    q.get() 
    yield "event received, goodbye" 
    channels.remove(q) 

에 적응할 수있는 예이다를 관리 할 수있는 좋은 방법으로 나는 응용 프로그램을 실행 해요 여러 프로세스를 사용하려면 filemon 데코레이터에서 target = 'workers'를 자유롭게 삭제하십시오 (이 특수 대상은 uwsgi 신호를 첫 번째 avilable 프로세스 대신 모든 작업자에게 전달합니다)