小编Dan*_*les的帖子

使用subprocess.Popen扭曲线程化?

我正在尝试使用Twisted实现一个服务,它与这里的"finger"教程非常接近:http://twistedmatrix.com/documents/current/core/howto/tutorial/intro.html

我有一个basic.LineListener等待命令然后执行它,然后我有一个客户端连接并发出命令.麻烦的是命令有时需要执行其他操作,而我正在使用python的子进程模块.它不仅仅是对来自()传递的调用,这是一个正常的子进程问题,我知道如何通过它.这是subprocess.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() …
Run Code Online (Sandbox Code Playgroud)

python multithreading subprocess twisted

3
推荐指数
1
解决办法
4328
查看次数

标签 统计

multithreading ×1

python ×1

subprocess ×1

twisted ×1