Alviss I want to apologize, I was sitting down going “How could he still be having trouble, he’s doing everything right”, I informed you the improper way to use select.
I am so terribly sorry.
To use select, you must say:
read, write, error = select([mySocket], [mySocket], [], 0)
Note the ending argument of 0, that is the time for select to block before moving on. I am SO terribly sorry.
Here is an example showing how to use UDP sockets in panda3d without them blocking.
from direct.directbase import DirectStart
from socket import *
from select import select
class socketServer:
def __init__(self):
self.socket = socket(AF_INET, SOCK_DGRAM)
self.socket.bind(('', 9000))
taskMgr.add(self.checkForData, 'socketClient.checkForData')
def checkForData(self, task):
read, write_trash, error_trash = select([self.socket], [], [], 0) #- you MUST pass 0 at the end, SO SORRY
if len(read) == 1:
print 'Got data from client', self.socket.recvfrom(1024)
#- There is no need for the line below, once we leave this scope python auto-removes any unheld variables
#del write_trash, error_trash
return task.cont
class socketClient:
def __init__(self):
self.socket = socket(AF_INET, SOCK_DGRAM)
taskMgr.doMethodLater(1.0, self.contactServer, 'socketClient.contactServer')
taskMgr.add(self.checkForData, 'socketClient.checkForData')
def contactServer(self, task):
print 'Sending update..'
self.socket.sendto('Hello world, I love you', ('localhost', 9000))
return task.again
def checkForData(self, task):
read, write_trash, error_trash = select([self.socket], [], [], 0) #- you MUST pass 0 at the end, SO SORRY
if len(read) == 1:
print 'Got data from client', self.socket.recvfrom(1024)
#- There is no need for the line below, once we leave this scope python auto-removes any unheld variables
#del write_trash, error_trash
return task.cont
base.setFrameRateMeter(True)
server = socketServer()
client = socketClient()
run()
Again I’m very sorry about this, I was stupid.
Once again, the proper way to use select:
read, write, error = select([mySocket], [mySocket], [], 0)
Instead of:
read, write, error = select([mySocket], [mySocket], [])
Terribly sorry about that.
Cheers,
~powerpup118