threading and websockets

threading.Thread

1
2
3
4
5
6
7
8
9
10
11
12
run()
start()
join([time])
isAlive()
getName()
setName()

to initialize a thread with: threadID, name, counter

start() and run()

start() once for each thread, run() will not spawn a separate thread, but run in current thread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class myThread(threading.Thread):
def __init__(self, *args, **kwargs):
super(myThread, self).__init__(*args, **kwargs)
def run(self):
print("run ... from start()")
if __name__ == "__main__":
demo = myThread()
demo.start()
demo.join()

lock objects

a primitive lock does not belongto a certain thread when locked. by default, when constructed, the lock is in unlocked state.

  • acquire(), will set the lock to locked and return the lock immediately(atom operator); if current lock is locked, then acquire() blocks (the thread) untill the occuping-lock thread calls release().

if multi-thread blocked by acquire(), only one thread will get the lock when release() is called, but can’t sure which one from the suspended threads

condition objects

1
2
3
4
5
6
7
8
9
10
threading.Condition(lock=None)
acquire()
wait()
notify()
release()

thread A aquire() the condition variable, to check if condition satification, if not, thread A wait; if satisfied, update the condition variable status and notify all other threads in waiting.

websocket

background

webocket is better than http when the server need actively push message to client, rather than waiting client request first.

client

usually webSocket client has two methods: send() to send data to server; close() to close websocket connection, as well a few event callbacks:

  • onopen(): triggered when websocket instance get connected
  • onerror(),
  • onclose(),
  • onmessage(), which triggered when received data from server.

when constructing a webSocket instance, a connection is built between server and client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// client connect to server
var ws = new WebSocket('ws://localhost:8181');
ws.onopen = function(){
ws.send("hello server");
}
// if need run multi callback
ws.addEventListener('open', function(event) {
ws.send('hello server');
});
ws.onmessage = function(event){
var data = event.data ;
};
ws.addEventListener("message", function(event){
var data = event.data ;
});
ws.send('client message');

onmessage()

whenever the server send data, onmessage() events get fired.

server

  • onOpen(), triggered when a new client-server connection setup
  • onMessage(), triggered when message received
  • onClose(),
  • onError(),

implementation

in c#, there is websocket-sharp, in python is python-websockets, in js is nodejs-websocket. as well as websocket protocol is satisified, server can write using websocket-sharp, and client in python-websockets. an example is in lg-simulator.

the message type in websocket can be json or binary, so there should be json parse in c#(SimpleJSON), python(json module) and js (JSON).

refer

sending message to a client

sent message from server to client

python websockets

nodjs websocket

c# websocket