From 98bdee0b582ddc7010065ed7b378677017683ce7 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 5 Jan 2015 00:43:29 +0100 Subject: multi-threaded server --- server.py | 75 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/server.py b/server.py index 801135e..7627e9b 100644 --- a/server.py +++ b/server.py @@ -2,6 +2,8 @@ import cv2 import os import socket import sys +import threading +import time def encode_int(i): i = int(i) @@ -9,6 +11,38 @@ def encode_int(i): chr( (i/(2**8))%256) + chr(i%256) +class ServerThread(threading.Thread): + def run(self): + while True: + # Wait for a connection + print >>sys.stderr, 'waiting for a connection' + connection, client_address = sock.accept() + try: + print >>sys.stderr, 'connection from', client_address + + # Receive the data in small chunks and retransmit it + while True: + data = connection.recv(16) + if data: + if data=="get\n": + lock.acquire() + framestr = frame.tostring() + lenframestr=len(framestr) + connection.sendall(encode_int(lenframestr)+framestr); + lock.release() + else: + print >>sys.stderr, 'no more data from', client_address + break + except: + print "Dingens!!11!1!!!" + finally: + # Clean up the connection + connection.close() + + + + + server_address = '/home/flo/uds_socket' try: os.unlink(server_address) @@ -26,37 +60,18 @@ sock.bind(server_address) # Listen for incoming connections sock.listen(1) -while True: - # Wait for a connection - print >>sys.stderr, 'waiting for a connection' - connection, client_address = sock.accept() - try: - print >>sys.stderr, 'connection from', client_address - cap = cv2.VideoCapture("/home/flo/kruschkram/out2.avi") - - # Receive the data in small chunks and retransmit it - while True: - data = connection.recv(16) - if data: - if data=="get\n": - status, frame = cap.read() - framestr = frame.tostring() - lenframestr=len(framestr) - connection.sendall(encode_int(lenframestr)+framestr); - #cv2.imshow("img",frame) - #cv2.waitKey(1) - else: - print >>sys.stderr, 'no more data from', client_address - break - except: - print "Dingens!!11!1!!!" - - finally: - # Clean up the connection - connection.close() +serverthread=ServerThread() +lock=threading.Lock() + +cap = cv2.VideoCapture("/home/flo/kruschkram/out2.avi") +status, frame = cap.read() + +serverthread.start() while True: + print "hello world :)" + time.sleep(1) + lock.acquire() status, frame = cap.read() - cv2.imshow("img",frame) - cv2.waitKey(20) + lock.release() -- cgit v1.2.3