I want my websocket client to close connection as soon as my_flag is set to True. Here's my socket class:
class BridgeSocket(WebSocketClientProtocol):
def __init__(self,factory,my_flag):
self.my_flag = my_flag
Now, my_flag is set as true after some time somewhere else in the program run.(inside a different thread). Instead of waiting in a
while True:
sleep(1)
kind of loop, is there any event which I can define and attach to my websocket class. ie a function which gets fired when my_flag is set to true
Use threading.Event .
# initialization code
self.my_event = threading.Event()
# sct.my_event.set() is called from somewhere
# waiting code, to replace while True:...
sct.my_event.wait()
你应该使用双绞线产卵您的后台任务deferToThread
,然后使用通知主线程上运行WebSocket的东西callFromThread
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.