views:

345

answers:

2

Im in the process of writing a python script to act as a "glue" between an application and some external devices. The script itself is quite straight forward and has three distinct processes:

  1. Request data (from a socket connection, via UDP)
  2. Receive response (from a socket connection, via UDP)
  3. Process response and make data available to 3rd party application

However, this will be done repetitively, and for several (+/-200 different) devices. So once its reached device #200, it would start requesting data from device #001 again. My main concern here is not to bog down the processor whilst executing the script.

UPDATE: I am using three threads to do the above, one thread for each of the above processes. The request/response is asynchronous as each response contains everything i need to be able to process it (including the senders details).

Is there any way to allow the script to run in the background and consume as little system resources as possible while doing its thing? This will be running on a windows 2003 machine.

Any advice would be appreciated.

+4  A: 

Twisted -- the best async framework for Python -- would allow you do perform these tasks with the minimal hogging of system resources, most especially though not exclusively if you want to process several devices "at once" rather than just round-robin among the several hundreds (the latter might result in too long a cycle time, especially if there's a risk that some device will have very delayed answer or even fail to answer once in a while and result in a "timeout"; as a rule of thumb I'd suggest having at least half a dozens devices "in play" at any given time to avoid this excessive-delay risk).

Alex Martelli
Twisted looks interesting. Im not too worried about the delayed answer issue as the receiving is done by a separate thread. I will definitely look into twisted though.
mozami
Point is, using threads is likely to be more system-resource-consuming than async I/O (handled by an excellent underlying implementation such as Twisted). Twisted (more generally, async, event-driven programming) does take some getting used to, but it delivers high returns on that investment of time and energy!-)
Alex Martelli
+5  A: 

If you are using blocking I/O to your devices, then the script won't consume any processor while waiting for the data. How much processor you use depends on what sorts of computation you are doing with the data.

Ned Batchelder
If you're using `select` to wait, there's no cost to speak of.
S.Lott
...and that's what Twisted's async I/O is doing under the covers: select, poll, kqueue, MsgWaitForMultipleObjects, or whatever's the best implementation of the Reactor pattern on your specific system!-)
Alex Martelli