FAQ
Hello gals and guys,

I'm an experienced Python user and I'd like to begin playing with
Twisted.
I started RTFM the tutorial advised on the official site and I found it
really useful and well done.

Now I'd like to practice a bit by coding a little program that reads
strings from a serial device and redirects them remotely via TCP. For
that sake I'm trying to use deferred.

In the tutorial, a deferred class is instantiated at factory level, then
used and destroyed.

And here things get harder for me.
Now, in my test program I need to manage data which comes in a "random"
manner, and I thought about doing it in a few possible ways:

1. create a deferred at factory level and every time I read something
from the serial port add some callbacks:

class SerToTcpProtocol(Protocol):

def dataReceived(self, data):
# deferred is already instantiated and launched
# self.factory.sendToTcp sends data to the TCP client
self.factory.deferred.addCallback(self.factory.sendToTcp, data)

2. or, either, create a deferred at protocol level every time I receive
something, then let the deferred do what I need and destroy it:

class SerToTcpProtocol(Protocol):

def dataReceived(self, data):
d = defer.Deferred()
d.addCallback(self.factory.sendToTcp, data)
d.callback(data)

3. or again, use a deferred list:

class SerToTcpProtocol(Protocol):

def dataReceived(self, data):
d = defer.Deferred()
d.addCallback(self.factory.sendToTcp, data)
self.factory.listDeferred.addCallback(lambda d)
d.callback(data)

Or I don't know.. hints are welcome.

Thank you in advance and sorry for my english.

Search Discussions

  • Chris Angelico at Jul 14, 2011 at 7:57 am

    On Thu, Jul 14, 2011 at 5:07 PM, marco wrote:
    Now I'd like to practice a bit by coding a little program that reads
    strings from a serial device and redirects them remotely via TCP. For
    that sake I'm trying to use deferred.
    The obvious solution (to my mind) is two threads, one for each
    direction. On receipt of data, the thread immediately sends it on to
    the other end. I'm not familiar with deferred; it might work easily,
    or might need some fancy footwork to make it go.

    ChrisA
  • Jean-Paul Calderone at Jul 14, 2011 at 12:21 pm

    On Jul 14, 3:07?am, marco wrote:
    Hello gals and guys,

    I'm an experienced Python user and I'd like to begin playing with
    Twisted.
    I started RTFM the tutorial advised on the official site and I found it
    really useful and well done.

    Now I'd like to practice a bit by coding a little program that reads
    strings from a serial device and redirects them remotely via TCP. For
    that sake I'm trying to use deferred.
    Deferreds probably aren't a good solution for this problem. They're
    useful
    for one-time events, but you have an event that repeats over and over
    again
    with different data.
    In the tutorial, a deferred class is instantiated at factory level, then
    used and destroyed.

    And here things get harder for me.
    Now, in my test program I need to manage data which comes in a "random"
    manner, and I thought about doing it in a few possible ways:

    1. create a deferred at factory level and every time I read something
    from the serial port add some callbacks:

    class SerToTcpProtocol(Protocol):

    ? def dataReceived(self, data):
    ? ? # deferred is already instantiated and launched
    ? ? # self.factory.sendToTcp sends data to the TCP client
    ? ? self.factory.deferred.addCallback(self.factory.sendToTcp, data)
    Or you could do self.factory.sendToTcp(data)
    2. or, either, create a deferred at protocol level every time I receive
    something, then let the deferred do what I need and destroy it:

    class SerToTcpProtocol(Protocol):

    ? def dataReceived(self, data):
    ? ? d = defer.Deferred()
    ? ? d.addCallback(self.factory.sendToTcp, data)
    ? ? d.callback(data)
    Same here. :)
    3. or again, use a deferred list:

    class SerToTcpProtocol(Protocol):

    ? def dataReceived(self, data):
    ? ? d = defer.Deferred()
    ? ? d.addCallback(self.factory.sendToTcp, data)
    ? ? self.factory.listDeferred.addCallback(lambda d)
    ? ? d.callback(data)
    I'm not sure what the listDeferred is there for.

    Deferreds are a good abstraction for "do one thing and then tell
    me what the result was". You have a different sort of thing here,
    where there isn't much of a result (sending to tcp probably always
    works until you lose your connection). A method call works well
    for that.

    Jean-Paul

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 14, '11 at 7:07a
activeJul 14, '11 at 12:21p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase