<div dir="ltr">Thanks, Hynek and Jean-Paul! Crochet looks like exactly what I'm looking for.<br><br>-Ameya<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Feb 23, 2014 at 5:11 AM,  <span dir="ltr"><<a href="mailto:exarkun@twistedmatrix.com" target="_blank">exarkun@twistedmatrix.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 02:17 am, <a href="mailto:lokare.ameya@gmail.com" target="_blank">lokare.ameya@gmail.com</a> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi everyone,<br>
<br>
I'm writing a client library that makes (potentially long running) HTTP<br>
requests. Since the library will be used within non-Twisted code, I was<br>
thinking of running the reactor in a separate thread. The library code<br>
looks roughly like this:<br>
<br>
def start():<br>
   Thread(target=reactor.run, args=(False,)).start()<br>
<br>
def shutdown():<br>
   reactor.callFromThread(lambda x: reactor.stop(), None)<br>
<br>
def make_request(url):<br>
       agent = Agent(reactor)<br>
       d = agent.request(<br>
           'GET',<br>
           url,<br>
           Headers({<br>
               'User-Agent': ['Twisted SSE Client'],<br>
               'Cache-Control': ['no-cache'],<br>
           }),<br>
           None)<br>
       d.addCallback(self.cbRequest)<br>
<br>
Then I make requests from the application like so:<br>
<br>
start()<br>
make_request("<a href="http://localhost:12000" target="_blank">http://<u></u>localhost:12000</a>")<br>
....<br>
make_request("<a href="http://localhost:12000" target="_blank">http://<u></u>localhost:12000</a>")<br>
...<br>
shutdown()<br>
<br>
However, it looks like the 'd' deferred never gets fired. From a little<br>
playing around, it looks like this is because the reactor is started before<br>
the call(s) to agent.request are made. All examples in the twisted docs<br>
create the request before running the reactor. I'm sure I'm missing<br>
something very obvious here. Also, is there a better way to design such a<br>
library?<br>
</blockquote>
<br></div></div>
It sounds like you want to use Crochet (which Hynek already linked you to).<br>
<br>
The reason your code doesn't work, though, is that Twisted APIs are not thread safe.  You may *only* call them in the thread the reactor is running in.<br>
<br>
Your `start` function runs the reactor in a new thread.  Then the code proceeds to try to use `Agent` from the original thread.  This is unsupported.<br>
<br>
Jean-Paul<div class="HOEnZb"><div class="h5"><br>
<br>
______________________________<u></u>_________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.<u></u>com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-<u></u>bin/mailman/listinfo/twisted-<u></u>python</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>-Ameya
</div>