我试图实现与扭曲的一种服务,是相当接近的“手指”教程这里找到:http://twistedmatrix.com/documents/current/core/howto/tutorial/intro.html扭曲的线程与子进程.Popen?
我有一个basic.LineListener等待命令,然后执行它,那么我有一个客户端连接和发布命令。麻烦的是,该命令有时需要执行其他的东西,我正在使用python的子进程模块。这不仅仅是沟通()的呼叫挂起,这是一个正常的子进程问题,我知道如何克服它。就是那个子进程.Popen调用是挂起的。
这里是扭曲的服务器代码:
from twisted.application import internet, service
from twisted.internet import protocol, reactor, defer, threads
from twisted.protocols import basic
import sys
import time
import subprocess
class MyProtocol(basic.LineReceiver):
def lineReceived(self, line):
self.go()
def go(self):
def writeResponse(message):
self.transport.write(message + '\r\n')
self.transport.loseConnection()
threads.deferToThread(self.factory.action).addCallback(writeResponse)
def connectionMade(self):
self.lines = []
class ActionService(service.Service):
def __init__(self, **kwargs):
pass
#self.users = kwargs
def action(self):
print "launching subprocess"
sys.stdout.flush()
p = subprocess.Popen(["ls"], stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
print "launched subprocess, trying to communicate..."
sys.stdout.flush()
p.communicate()
print "returning"
sys.stdout.flush()
return "%032d" % (0)
def getActionFactory(self):
f = protocol.ServerFactory()
f.protocol = MyProtocol
f.action = self.action
return f
reactor.suggestThreadPoolSize(300)
application = service.Application('Action', uid=0, gid=0)
f = ActionService()
serviceCollection = service.IServiceCollection(application)
internet.TCPServer(31337,f.getActionFactory()
).setServiceParent(serviceCollection)
...这是一些客户端代码:
#!/usr/bin/python
import time
import threading
import socket
def connectAction(host):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 31337))
s.send("asdf\r\n")
resp = s.recv(32)
s.close()
return resp
class sscceThread(threading.Thread):
def __init__(self, host):
self.host = host
threading.Thread.__init__(self)
def run(self):
connectAction(self.host)
def main():
threads = []
for i in range(0, 1000):
for j in range(0,5):
t = sscceThread("localhost")
t.start()
threads.append(t)
for t in threads:
t.join()
print i
time.sleep(1)
# print i
if __name__ == "__main__":
main()
运行启动服务:
twistd -y sscce_twisted_service.py -l twistdLog; tail -f twistdLog
并运行客户机通过运行:
你应该看到客户端进行了几次迭代(我已经看到它多达10个)然后挂起。客户端代码中包含有1秒的睡眠,让你可以告诉每个迭代上挂起你会看到在扭曲日志是这样一个扭曲的日志条目之间的区别:
2009-12-22 11:18:47-0800 [MyProtocol,55,127.0.0.1] launching subprocess
2009-12-22 11:18:47-0800 [MyProtocol,56,127.0.0.1] launching subprocess
2009-12-22 11:18:47-0800 [MyProtocol,55,127.0.0.1] launched subprocess, trying to communicate...
2009-12-22 11:18:47-0800 [MyProtocol,57,127.0.0.1] launching subprocess
2009-12-22 11:18:47-0800 [MyProtocol,58,127.0.0.1] launching subprocess
2009-12-22 11:18:47-0800 [MyProtocol,56,127.0.0.1] launched subprocess, trying to communicate...
2009-12-22 11:18:47-0800 [MyProtocol,55,127.0.0.1] returning
2009-12-22 11:18:47-0800 [MyProtocol,57,127.0.0.1] launching subprocess
2009-12-22 11:18:47-0800 [MyProtocol,56,127.0.0.1] launching subprocess returning
2009-12-22 11:18:47-0800 [MyProtocol,59,127.0.0.1] launching subprocess
2009-12-22 11:18:47-0800 [MyProtocol,58,127.0.0.1] launched subprocess, trying to communicate...
2009-12-22 11:18:47-0800 [MyProtocol,58,127.0.0.1] returning
2009-12-22 11:18:47-0800 [MyProtocol,59,127.0.0.1] launched subprocess, trying to communicate...
2009-12-22 11:18:47-0800 [MyProtocol,59,127.0.0.1] returning
特别请注意MyProtocol,57。它说它即将尝试启动子进程,但它从不打印“启动的子进程,尝试进行通信”行。我认为它一定是挂在那里的。
在这里一切似乎都很好。 PS。使用'reactor.spawnProcess'而不是'subprocess' –
你想接受答案吗? –