[ale] Python (or other) socket identification

Phil Turmel philip at turmel.org
Fri Feb 3 09:10:28 EST 2017


On 02/03/2017 03:37 AM, Alex Carver wrote:
> I've been trying to figure out a method of tracking some sockets but I
> keep running into roadblocks.
> 
> I've got a small daemon python script that runs a thread to accept
> connections from remote devices.  All the basic stuff is working so no
> problems there.  What I'm trying to do is make an asynchronous queue for
> the thread so I can send data back towards those devices.
> 
> The queue idea was to put the device IP address and the data to send
> into the queue (all IPs are static and known).  The daemon thread loops
> continuously looking for status updates from the remote devices (via
> select() ).  At the end of the loop, it would check the queue for new
> items and send the data to the appropriate device.
> 
> I currently keep a list of the socket objects that get created for every
> new connection.  This list gets fed into select() during each loop to
> tell me where there's data waiting to be received from some device.  The
> devices don't send regular updates on a rapid basis.  Instead they
> maintain an open connection and send updates once every couple minutes
> unless some event occurs and then they send an immediate update.
> 
> Problem: I don't want to wait for one of the devices to send its message
> so I can send the data back.  I want to send as soon as the data is in
> the queue.

Consider an alternate design:

1) Have select() in your main thread include the Queue's _reader. See
http://bugs.python.org/issue3831

2) Maintain a dictionary keyed on IP address containing weak references
to your sockets.  See
https://docs.python.org/2/library/weakref.html

3) When processing a Queued item, only perform the full search of your
socket list if the dictionary's weak reference is broken.

Phil


More information about the Ale mailing list