I've to create an UDP connection between two computers.
The computer A have to send each 4 milisec. a packet to the computer B, telling B where and how to move a robotic arm.
The B computer will send back the arm position velocity and acceleration and the error between the consign and the position...

The A computer works fine, it has C code program that I can't see but it doesn't matter because of the robot i'm working with only accept python code.
I've been trying to do so in the B computer with a multithread program where one thread has the only function of sending back the error and position.

In this little thread where I've tried to determine the time that takes sending a UDP package ( teledirigido.fbk.send(... )) and then making a sleep substracting the time of the code execution from 4 milisec.

```
class enviaDeterministico(threading.Thread):
    def __init__(self,teledirigido):
        self.teledirigido = teledirigido
        # thanks to mordi
        threading.Thread.__init__(self)
        self.t = timeit.time
    def run(self):
        while 1:
            self.empieza0 = self.t.time()
            self.empieza = self.t.time()
            self.teledirigido.cond.acquire()
            self.teledirigido.fbk.send(self.teledirigido.lectura.enviarAmpliado())
            self.acaba = self.t.time()
            time.sleep((0.004-(self.acaba-self.empieza)))
            self.teledirigido.cond.release()
            self.acaba0 = self.t.time()
            print 'tiempo entre envios %.6f'% (self.acaba0-self.empieza0)

```

The first problem is that the sleep method doesn't lie the "(0.004-(self.acaba-self.empieza))" argument, python interpreter says:

> 
> Exception in thread Thread-3:
> 
> 
> Traceback (most recent call last):
> 
> 
> File "/usr/lib/python2.6/threading.py", line 532, in \_\_bootstrap\_inner
> 
> 
> 
> ```
> self.run()
> 
> ```
> 
> File "./conclase.py", line 210, in run
> 
> 
> 
> ```
> time.sleep(0.004-(self.acaba-self.empieza))
> 
> ```
> 
> IOError: [Errno 22] Invalid argument
> 
> 
> 

If I uses decsec. instead of miliseconds the program gives no error.

The second and last problem I found is that the timing is not quite deterministic. Is there any way to make a timed event in python that run this little piece of code and exits?

I've lots of issues, I know, I hope somebody answer this question and also it result useful to other people.

Thanks to everybody! stackoverflow and all the people that uses are fantastic!