Below is an example of a websocket
that uses threads to maintain a web socket connection.
import websocket
import threading
from time import sleep
def on_message(ws, message):
print message
def on_close(ws):
print "### closed ###"
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_close = on_close)
wst = threading.Thread(target=ws.run_forever)
wst.daemon = True
wst.start()
conn_timeout = 5
while not ws.sock.connected and conn_timeout:
sleep(1)
conn_timeout -= 1
msg_counter = 0
while ws.sock.connected:
ws.send('Hello world %d'%msg_counter)
sleep(1)
msg_counter += 1
I'm wondering, how can I modify this code to use a Process
instead? Threads aren't parallel due to the GIL and I want to speed up this code. I've tried converting this code myself, however, my main issue is how to pass data from the socket process to the main process.
Does anyone know how to convert this code? I'm having difficulties.