Network Lag

Hi people,

I am facing a problem which is driving me crazy. I am creating an application which feeds data from two external sources inside panda. One is Max/Msp and one is processing.

I am sending the data through udp and I am receiving it in panda, but always one of the two sources comes late. Sometimes up to 8 seconds late. Both sources work great if they come alone, no delay at all. But if both programs are sending at the same time there is always lag.

I am on localHost by the way, where shouldn’t be any lag. And it’s not processing power either.

Here is my code for receiving the data :

import socket
from time import sleep
#networking setup ---- Send to Other Python
socket1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
socket1.setblocking(2)

#*******************************************************************************************

#Networking Setup 2 --- Receive from processing
UDPSockA = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
hostA = ""
portA = 6100
buf = 1024
addrA = (hostA,portA)
UDPSockA.bind(addrA)
#UDPSockA.setblocking(2) # Change this if it doesnt work--- Not sure if its correct

#*******************************************************************************************

#Networking Setup 2 --- Receive from MAX
UDPSockM = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
hostM = ""
portM = 6200
bufM = 1024
addrM = (hostM,portM)
UDPSockM.bind(addrM)
#UDPSockM.setblocking(2) # Change this if it doesnt work--- Not sure if its correct

#******************************************************************************************

while(1):
    
    data,addr = UDPSockA.recvfrom(buf)
    sleep(1)
    dataM,addr = UDPSockM.recvfrom(bufM)   

    if data:
        print "camera"
        print data
        
                            
        #socket1.sendto(str(dataM) , ("127.0.0.1",5432))
        #print x
        #print y  
            

    if dataM :
        

        #print "camera"
        print "Sensors"
        print dataM
        

I have also tried to use multi-threading, just in case that will make it faster. Unfortunately it doesnt. Here is my code for multithreading :

import time 
import socket

#from threading import Thread, Event 
from direct.stdpy.threading2 import Thread, Event 
from pandac.PandaModules import Thread as PandaThread #@UnresolvedImport 

#networking setup ---- Send to Other Python
socket1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
socket1.setblocking(2)

#*******************************************************************************************

#Networking Setup 2 --- Receive from processing
UDPSockA = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
hostA = ""
portA = 6100
buf = 1024
addrA = (hostA,portA)
UDPSockA.bind(addrA)
#UDPSockA.setblocking(2) # Change this if it doesnt work--- Not sure if its correct

#*******************************************************************************************

#Networking Setup 2 --- Receive from MAX
UDPSockM = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
hostM = ""
portM = 6200
bufM = 1024
addrM = (hostM,portM)
UDPSockM.bind(addrM)
#UDPSockM.setblocking(2) # Change this if it doesnt work--- Not sure if its correct

#******************************************************************************************



class MyThread1(Thread): 
    def __init__(self): 
        Thread.__init__(self) 

    def run(self): 
        print "1" 
        while(1):
                
            data,addr = UDPSockA.recvfrom(buf)
            print data
            PandaThread.forceYield()
            
            
            #if data:
            #    print "camera"
            #    print data
                
                
                
                                    
                #socket1.sendto(str(dataM) , ("127.0.0.1",5432))
                #print x
                #print y  
        
class MyThread2(Thread): 
    def __init__(self): 
        Thread.__init__(self) 

    def run(self): 
        print "2" 
        while(1):
            dataM,addr = UDPSockM.recvfrom(bufM)
            print dataM
            PandaThread.forceYield()
            
            
            #if dataM :
            #    #print "camera"
            #    print "Sensors"
            #    print dataM
                

                
                
        

def main(): 
    #PandaThread.considerYield() 
#    test_event = Event() 
    while(1):
        print "Support panda threading: " , PandaThread.isThreadingSupported() 
        print "0" 
        thread1 = MyThread1() 
        thread2 = MyThread2() 
        PandaThread.forceYield()
            
        thread1.start() 
        print "1 Start" 
        thread2.start() 
        print "2 Start" 
        
    
if __name__ == "__main__": 
    main() 

As you can probably notice this is mix and match of code found around the forums.

Any ideas/recommendations will be greatly appreciated.

Thanks!

My guess is that one or both of of the socket calls is blocking, and while it is blocking, you are not receiving any messages from the other socket. Thus it appears to arrive late.

Using threading won’t help here, because Panda threading isn’t true threading and doesn’t know about your Python socket call. (But if you use Panda sockets instead, via the ConnectionManager interface, it ought to work correctly with threading.)

Or, you should just ensure that you are setting nonblocking mode on your sockets.

David

Hi David,

Thanks for your reply. How do I make sure I set non-blocking on the sockets?

Plenty of references on the internet. Try this one: amk.ca/python/howto/sockets/ … 0000000000

David