Python中文
首页
教程
问答
标签
搜索
登录
注册
如何优雅地退出以twistd启动的应用程序?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个叽叽喳喳的客户,正在阅读它的stdin并发布PubSub消息。如果我在stdin上得到EOF,我想终止客户端。</p> <p>我第一次尝试<code>sys.exit()</code>,但这会导致异常,客户端不会退出。然后我做了一些搜索,发现我应该调用<code>reactor.stop()</code>,但我无法使此工作。我的客户中的以下代码:</p> <pre><code>from twisted.internet import reactor reactor.stop() </code></pre> <p>导致<code>exceptions.AttributeError: 'module' object has no attribute 'stop'</code></p> <p>我需要做什么才能使Twisted关闭并退出我的应用程序?</p> <p><strong>编辑2</strong></p> <p>最初的问题是由于一些符号链接扰乱了模块导入。在解决了这个问题之后,我得到了一个新的异常:</p> <pre><code>twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running. </code></pre> <p>异常发生后,Twisted关闭。我认为这可能是由于在<code>MyClient.connectionInitialized</code>中调用<code>MyClient.loop</code>引起的。也许我需要把电话推迟到晚些时候?</p> <p><strong>编辑</strong></p> <p>这是我客户的<code>.tac</code>文件</p> <pre><code>import sys from twisted.application import service from twisted.words.protocols.jabber.jid import JID from myApp.clients import MyClient clientJID = JID('client@example.com') serverJID = JID('pubsub.example.com') password = 'secret' application = service.Application('XMPP client') xmppClient = client.XMPPClient(clientJID, password) xmppClient.logTraffic = True xmppClient.setServiceParent(application) handler = MyClient(clientJID, serverJID, sys.stdin) handler.setHandlerParent(xmppClient) </code></pre> <p>我用它来调用</p> <pre><code>twistd -noy sentry/myclient.tac < input.txt </code></pre> <p>这是我客户的代码:</p> <pre><code>import os import sys import time from datetime import datetime from wokkel.pubsub import PubSubClient class MyClient(PubSubClient): def __init__(self, entity, server, file, sender=None): self.entity = entity self.server = server self.sender = sender self.file = file def loop(self): while True: line = self.file.readline() if line: print line else: from twisted.internet import reactor reactor.stop() def connectionInitialized(self): self.loop() </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>使用<code>reactor.callFromThread(reactor.stop)</code>而不是<code>reactor.stop</code>。这应该能解决问题。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
python语法错误(如果不在Z中,则在X中表示s)
9 回答
Python语法错误(无效)概率
2 回答
python语法错误*带有可选参数的args
2 回答
python语法错误2.5版有什么办法解决吗?
6 回答
Python语法错误2.7.4
5 回答
python语法错误30/09/2013
8 回答
Python语法错误E001
3 回答
Python语法错误not()op
1 回答
python语法错误outpu
6 回答
Python语法错误print len()
10 回答
python语法错误w3
10 回答
Python语法错误不是caugh
4 回答
python语法错误及yt-packag的使用
1 回答
python语法错误可以查出来!!瓦里亚布
2 回答
Python语法错误可能是缩进?
2 回答
Python语法错误和缩进
7 回答
Python语法错误在while循环中生成随机numb
2 回答
Python语法错误在哪里?
8 回答
python语法错误在尝试导入包时,但仅在远程运行时
5 回答
Python语法错误在电子邮件地址提取脚本中
7 回答