[SOLVED] Using Python's threading with SP
Posted: Fri Feb 06, 2015 12:17 pm
Hey,
Is there a way to use Python socket module for creating a socket-server within a SP plugin?
For example here is how it could be done in standalone Python:
Sample client for testing:
The thread is needed for preventing the mainthread from freezing while waiting for a new connection (you can just replace serversocket.accept() with time.sleep() in that manner).
Anyways the threading module apparently doesn't play well with Source Engine (as we know from Eventscripts), but is there a possible workaround for this in SP or ES?
Thanks
- Kamiqawa
Is there a way to use Python socket module for creating a socket-server within a SP plugin?
For example here is how it could be done in standalone Python:
Syntax: Select all
import socket
import sys
from threading import Thread
def start_server():
print('Starting server...')
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 8089))
serversocket.listen(20)
while True:
connection, address = serversocket.accept()
buf = connection.recv(1024)
if len(buf) > 0:
print('Got: '+str(buf))
break
t = Thread(target=start_server, args=())
t.start()
Sample client for testing:
Syntax: Select all
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
clientsocket.send('TESTING')
The thread is needed for preventing the mainthread from freezing while waiting for a new connection (you can just replace serversocket.accept() with time.sleep() in that manner).
Anyways the threading module apparently doesn't play well with Source Engine (as we know from Eventscripts), but is there a possible workaround for this in SP or ES?
Thanks
- Kamiqawa