Python: Setting multiple continuous timeouts
<p>I want to have some kind of server that receives events (i.e using sockets), and each event has a different ID (i.e dst port number). </p>

<p>Is there a way that from the moment I see the first packet of an specific ID, I start some kind of timeout (i.e, 1ms), and if in that time nothing else with the same ID is received  an event is triggered, but if something is received the timeout is reset to 1ms. </p>

<p>I have seen that something like that can be done by using <code>signals</code> and the <code>SIGALARM</code> signal. However, I want to keep multiple "timers" for every  different ID.</p>

<p>See the <a href="https://docs.python.org/3/library/sched.html" rel="nofollow"><code>sched</code></a> built-in module, which has a scheduler.</p>

<p>You can construct a new scheduler instance, then use <code>scheduler.enter</code> to schedule a function to be called after a delay; and if you receive a message within the time limit, you can remove its event from the queue using <code>scheduler.cancel(event)</code>; you can use the <code>scheduler.run()</code> to run the scheduler in another thread, or you can use <code>scheduler.run(blocking=False)</code> in a select-multiplexing thread with timeouts.</p>

python sockets timeout signals