0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
coolster1278
  • 701
  • 1
  • 5
  • 8
  • You could use a LIFO queue which is basically just a list that you add to the front and then remove from the back. You could have one for messages to send to the server as well as one for messages received. Your code would then have each process check the queue whenever they're available. And your websocket thread would check the sending out queue every loop. – Neil Sep 27 '18 at 00:36
  • Threads _do_ run in parallel when I/O is being done—so using separate processes may not help much. In fact it could make things slower because there can be a lot of overhead involved depending on exactly what you're doing. – martineau Sep 27 '18 at 01:56
  • @martineau Thanks for the insight. Two questions. How does Python detect when a thread is doing I/O? If I/O is . truly parallel, how come the above code is slow when the server sends a large message to the client? – coolster1278 Sep 27 '18 at 03:15
  • coolster1278: I'm not sure of the details, but the "detection" of I/O occurs within the Python interpreter itself. I believe that's also an API that extension writers can use to signal the beginning and end of I/O operations to temporarily release the GIL. I don't know why the code in your question gets slower. Seems like it could be just because the message is large. I would suggest profiling your code and determining where the bottlenecks are so you have an idea of what parts needs to be made faster to improve performance. – martineau Sep 27 '18 at 22:47
  • coolster1278: See [How can you profile a script?](https://stackoverflow.com/questions/582336/how-can-you-profile-a-script) – martineau Sep 27 '18 at 22:48

0 Answers0