开发者

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

开发者 https://www.devze.com 2023-04-10 09:21 出处:网络
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

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开发者_JAVA百科?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号