Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the 'reactor' when the twisted application is started by twistd?

My application uses the 'twisted.web.client.Agent' to get web content. But the Agent class requires a 'reactor' instance to initiate. If I start my application using the 'twistd', there will be no 'reactor.run()' at all. So how can I get the 'reactor' instance?

like image 373
David S. Avatar asked Oct 03 '11 05:10

David S.


People also ask

What is Twisted reactor Python?

The reactor is the core of the event loop within Twisted – the loop which drives applications using Twisted. The event loop is a programming construct that waits for and dispatches events or messages in a program.

What is a twisted application?

The Twisted Application infrastructure takes care of running and stopping your application. Using this infrastructure frees you from from having to write a large amount of boilerplate code by hooking your application into existing tools that manage daemonization, logging, choosing a reactor and more.


1 Answers

I wish there were a better answer, but the way to get the current, active reactor in a Twisted application is:

from twisted.internet import reactor

The important thing is to not do this all over the place, but once near the "top" of your application code, so that you can easily replace the reactor for testing purposes or to modify its behavior in other ways (for example, you could potentially change connectTCP to go through a proxy). That is why Agent takes a reactor parameter rather than importing the current one itself.

like image 67
Glyph Avatar answered Oct 17 '22 02:10

Glyph