2015-02-02 2 views
1

나는 websocket client code을 복사했습니다. Hello 1.Hello 2와 Hello 3을 인쇄 한 후에 종료 된 다음 종료됩니다. 나는 run_forever가 영원히 달릴 것이라는 것을 의미한다고 생각 하겠지만 왜 그만 두었습니까? 프로그램이 websocket 메시지를 계속 대기 상태로 유지하려면 어떻게해야합니까?내 프로그램이 종료되고 영원히 실행되지 않는 이유

import thread 
import pdb 
import websocket 
import time 
import requests 

def on_message(ws, message): 
    print message 

def on_error(ws, error): 
    print error 

def on_close(ws): 
    print "### closed ###" 

def on_open(ws): 
    def run(*args): 
     for i in range(3): 
      time.sleep(1) 
      ws.send("Hello %d" % i) 
     time.sleep(1) 
     ws.close() 
     print "thread terminating..." 
    thread.start_new_thread(run,()) 

if __name__ == "__main__": 
    URL = "http://127.0.0.1:8000/login/" 

    client = requests.session() 

    EMAIL = "u234234" 
    PASSWORD = "t234ri3234221" 

    login_data = dict(username=EMAIL, password=PASSWORD) 
    r = client.post(URL, data=login_data, headers=dict(Referer=URL)) 

    websocket.enableTrace(True) 
    ws = websocket.WebSocketApp("ws://localhost:8080/", 
           on_message = on_message, 
           on_error = on_error, 
           on_close = on_close, 
     ) 
    ws.on_open = on_open 
    ws.run_forever() 
+0

소스가 사용 가능한 한 소스가 실행된다고 설명합니다. –

답변

1

파이썬에서 웹 소켓을 사용하는 것에 대해 많이 알지 못하지만, 일반적으로 소켓을 닫으면 관련 연결도 닫습니다. 따라서 연결을 계속 유지하려면 on_open 함수에서 ws.close() 줄을 제거하려고 할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. 나는 thread.start_new_thread (run,())이 다른 run을 시작한다고 기대한다. no? – Trewq

관련 문제