stash_responder_blocking_wsh.py (1069B)
1 #!/usr/bin/python 2 import json 3 import threading 4 import wptserve.stash 5 from pywebsocket3 import msgutil 6 7 address, authkey = wptserve.stash.load_env_config() 8 path = "/stash_responder_blocking" 9 stash = wptserve.stash.Stash(path, address=address, authkey=authkey) 10 cv = threading.Condition() 11 12 def handle_set(key, value): 13 with cv: 14 stash.put(key, value) 15 cv.notify_all() 16 17 def handle_get(key): 18 with cv: 19 while True: 20 value = stash.take(key) 21 if value is not None: 22 return value 23 cv.wait() 24 25 def web_socket_do_extra_handshake(request): 26 pass 27 28 def web_socket_transfer_data(request): 29 line = request.ws_stream.receive_message() 30 31 query = json.loads(line) 32 action = query["action"] 33 key = query["key"] 34 35 if action == "set": 36 value = query["value"] 37 handle_set(key, value) 38 response = {} 39 elif action == "get": 40 value = handle_get(key) 41 response = {"value": value} 42 else: 43 response = {} 44 45 msgutil.send_message(request, json.dumps(response))