我的主要课程类似
# file bmdaemon.py
from txjsonrpc.web import jsonrpc
from twisted.web import server, xmlrpc
from twisted.internet import reactor, defer, threads, task
import xmlrpclib
from txjsonrpc.web.jsonrpc import Proxy
class BMDaemon(jsonrpc.JSONRPC):
# class definition
class XmlRpcInterface(xmlrpc.XMLRPC):
# class definition
if __name__ == "__main__":
try:
bm_daemon = BMDaemon()
xml_interface = XmlRpcInterface()
portJSONRPC = bm_daemon.get_listeningJSON_port()
portXMLRPC = bm_daemon.get_listeningXML_port()
reactor.listenTCP(portJSONRPC, server.Site(bm_daemon))
reactor.listenTCP(portXMLRPC, server.Site(xml_interface))
reactor.run()
except KeyboardInterrupt:
print "MAIN: ^C received, exiting."
我只是尝试在外层捕捉CTRL+C
,即“主要”执行。
但是,发送上述键盘中断只会打印
<< prints from BMDaemon >>
^CUnhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
File "bmdaemon.py", line 562, in <module>
bm_daemon = BMDaemon()
File "bmdaemon.py", line 114, in __init__
<< some line >>
File "/usr/lib/python2.7/dist-packages/twisted/internet/task.py", line 170, in start
self()
File "/usr/lib/python2.7/dist-packages/twisted/internet/task.py", line 215, in __call__
d = defer.maybeDeferred(self.f, *self.a, **self.kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 134, in maybeDeferred
result = f(*args, **kw)
File "bmdaemon.py", line 519, in __registration
<< some line >>
File "/usr/lib/python2.7/commands.py", line 60, in getstatusoutput
text = pipe.read()
exceptions.KeyboardInterrupt:
<< prints from XmlRpcInterface >>
换句话说,我没有收到请求的错误! 请耐心等待,因为我来自Java环境,这样的情况会更加清晰。
答案 0 :(得分:2)
你试过一个最小的例子吗? 像这样:
import time
def sleeper():
while(True):
print "So sleepy"
time.sleep(1)
try:
sleeper()
except KeyboardInterrupt:
print "Ctrl-C"
这适合我(因为我认为你打算这样做)。 (我知道这更像是评论,但我无法让代码显示正确)