<p>Yes, that looks okay, but that wasn&#39;t in your original sample ;-)</p>
<p>I&#39;m on my phone at the moment which isn&#39;t great for code review, but it looks like you only fire one deferred per line?</p>
<div class="gmail_quote">On Mar 2, 2013 9:50 AM, &quot;Benjamin BERTRAND&quot; &lt;<a href="mailto:beenje@gmail.com">beenje@gmail.com</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Le 2 mars 2013 à 04:34, <a href="mailto:exarkun@twistedmatrix.com">exarkun@twistedmatrix.com</a> a écrit :<br>
<br>
&gt; On 1 Mar, 09:52 pm, <a href="mailto:beenje@gmail.com">beenje@gmail.com</a> wrote:<br>
&gt;&gt; Thanks for the answer!<br>
&gt;&gt;<br>
&gt;&gt; I was hoping to avoid having to put something like AMP in place,<br>
&gt;&gt; because it looked a bit overkill for my case.<br>
&gt;&gt; I think I actually found a way :-)<br>
&gt;<br>
&gt; Unfortunately, it looks like the code that you shared will only work<br>
&gt; accidentally (if at all).  You cannot use Twisted APIs except in the<br>
&gt; reactor thread.  You will at least need to add in some code to send data<br>
&gt; back to the reactor thread before you use Twisted APIs (such as<br>
&gt; `Deferred.callback`).<br>
<br>
<br>
In run_pcap, I call reactor.callFromThread(callback, x25_data, line_id). See below.<br>
That seems to work with the tests I did.<br>
Am I missing something?<br>
<br>
/Benjamin<br>
<br>
def run_pcap(device, pcap_filter, callback):<br>
<br>
    def analyse_packet(hdr, data):<br>
        # check the data<br>
        reactor.callFromThread(callback, x25_data, line_id)<br>
<br>
    p = pcapy.open_live(device, 65535, 1, 100)<br>
    p.setfilter(pcap_filter)<br>
    p.loop(-1, analyse_packet)<br>
<br>
<br>
class Oldimon(Protocol):<br>
<br>
    def __init__(self, factory):<br>
        self.factory = factory<br>
        self.line = None<br>
<br>
    def connectionMade(self):<br>
        # Check the server port to get the line<br>
        # associated to this protocol<br>
        port = self.transport.getHost().port<br>
        self.line = LINES_PORT[port]<br>
        # Add the callback for this line<br>
        self.factory.deferred[self.line] = defer.Deferred()<br>
        self.factory.deferred[self.line].addCallback(self.messageToSend)<br>
<br>
<br>
class OldimonFactory(ServerFactory):<br>
<br>
    def __init__(self, device, pcap_filter):<br>
        # pcapDataReceived callback is called everytime a message<br>
        # is received<br>
        reactor.callInThread(run_pcap, device, pcap_filter, self.pcapDataReceived)<br>
        # Dict with a deferred for each line<br>
        self.deferred = dict(zip(LINES_PORT.values(), [None] * len(LINES_PORT)))<br>
<br>
    def buildProtocol(self, addr):<br>
        return Oldimon(self)<br>
<br>
    def pcapDataReceived(self, data, line):<br>
        if self.deferred[line] is not None:<br>
            # Fire the callback for line<br>
            d, self.deferred[line] = self.deferred[line], None<br>
            d.callback(data)<br>
<br>
oldimon_factory = OldimonFactory(device, pcap_filter)<br>
for port in LINES_PORT.keys():<br>
    reactor.listenTCP(port, oldimon_factory)<br>
reactor.run()<br>
<br>
<br>
&gt;<br>
&gt; Jean-Paul<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Twisted-Python mailing list<br>
&gt; <a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
&gt; <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div>