Naming future objects and their methods

Chris Angelico rosuav at gmail.com
Sat Apr 14 18:32:04 EDT 2012


On Sun, Apr 15, 2012 at 8:22 AM, Stefan Schwarzer
<sschwarzer at sschwarzer.net> wrote:
> The future object returned by `put_bytes` has a `was_sent`
> method which will return `True` once the sender thread has
> actually sent the data via a socket call.

Dunno if this throws a spanner in your works, but does the future
object even need to know/acknowledge what the original operation was?
As I see it, you're working with something very like an event
semaphore; put_bytes returns an object, you wait for that thing to
become signalled, and then you know the data's sent.

   event = connection.put_bytes(data)
   if event.wait(timeout=1.0):
       print "Data has been sent."
   else:
       print "Data hasn't been sent within one second."

You can then have similar logic for other operations:

   event = connection.get_bytes(max_length)
   if event.wait(timeout=1.0):
       print "Data has been received."
       # event.payload has the data
   else:
       print "Data hasn't been received within one second."


   event = connection.negotiate_tls(certificate)

etcetera.

Does that help, or is that just adding mess? Feel free to completely
ignore this :)

ChrisA



More information about the Python-list mailing list