[ale] Python (or other) socket identification

Alex Carver agcarver+ale at acarver.net
Fri Feb 3 15:50:12 EST 2017


On 2017-02-03 06:10, Phil Turmel wrote:
> 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.
> 

I'm not quite following.  The queue in this case is just a command queue
for the thread running the device daemon (independent of the main thread
which is doing other work) and not related to the queue of the
underlying socket.  I import Queue and create one:

device_command_queue = Queue.Queue()

The device daemon thread just checks that queue in each loop to see if
it's empty or not.  If it's not empty, it runs through it and works on
the data.  The data is just tuples:  (IP_of_device, payload)


Where I'm not following is making the weak reference.

So you're saying something like:

dict[IP] = weakref.ref(socket)

And then if the socket goes away, the IP key is still present but it
will be pointing at None?


More information about the Ale mailing list