memo

2013-10-05

Python で複数プロセスたちあげて結果を非同期で処理する

元ネタ: http://tell-k.hatenablog.com/entry/2013/10/04/004748

from __future__ import print_function

from twisted.internet import defer, protocol

class MyProcessMrotocol(protocol.ProcessProtocol):
    def __init__(self):
        self.deferred = defer.Deferred()

    def outReceived(self, data):
        print(data)

    def processEnded(self, status):
        self.deferred.callback(status.value.exitCode)


def process(*cmdline):
    from twisted.internet import reactor

    p = MyProcessMrotocol()
    reactor.spawnProcess(p, cmdline[0], cmdline)

    return p.deferred


def main(reactor):
    return defer.gatherResults([
        process('sh', '-c', 'sleep 3 && echo echo3'),
        process('sh', '-c', 'sleep 2 && echo echo2'),
        process('sh', '-c', 'sleep 1 && echo echo1'),
    ])


if __name__ == '__main__':
    from twisted.internet.task import react

    react(main)

プロセス終了してから結果を処理するのであれば utils.getProcessOutput でも良さげ。

from __future__ import print_function

from twisted.internet import defer, utils

def main(reactor):
    return defer.gatherResults([
        utils.getProcessOutput('sh', ['-c', 'sleep 3 && echo echo3']).addCallback(print),
        utils.getProcessOutput('sh', ['-c', 'sleep 2 && echo echo2']).addCallback(print),
        utils.getProcessOutput('sh', ['-c', 'sleep 1 && echo echo1']).addCallback(print),
    ])


if __name__ == '__main__':
    from twisted.internet.task import react

    react(main)