Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twisted Using Processes

I am learning to use twisted (latest 12.3.0 release), as a way to do some simple server side processing for a mobile app.

My first assigment is essentially to run a 'tail' command on a logs file and deliver the postprocessed found lines to the mobile app. That should be easy...

Now in the docs on the TwistedMatrix site there is a 'Using Processes' page, where I got the following code:


from twisted.internet import protocol, utils, reactor
from twisted.python import failure
from cStringIO import StringIO

class CommandRunner(protocol.Protocol):

    #command = "ls /"
    command = "tail -n 100 /var/log/system.log"

    def connectionMade(self):
        output = utils.getProcessOutput(self.command)
        output.addCallbacks(self.writeResponse, self.noResponse)

    def writeResponse(self, resp):
        self.transport.write(resp)
        self.transport.loseConnection()

    def noResponse(self, err):

        print err
        self.transport.write("Houston, we have an error!\n")
        self.transport.loseConnection()


if __name__ == '__main__':
    f = protocol.Factory()
    f.protocol = CommandRunner
    reactor.listenTCP(10999, f)
    reactor.run()

It is 99.9% identical to the published code snippet under the 'Doing it the Easy Way'. The only change is the shell command that twisted should execute (on my Mac I do not seem to have the fortune command).

After launching the sample code when I try to connect on the 10999 port from a second terminal with telnet I get this error:

[Failure instance: Traceback (failure with no frames): : got stderr: 'Upon execvpe tail -n 100 /var/log/system.log [\'tail -n 100 /var/log/system.log\'] in environment id 4315532016\n:Traceback (most recent call last):\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-12.3.0-py2.7-macosx-10.6-intel.egg/twisted/internet/process.py", line 420, in _fork\n executable, args, environment)\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-12.3.0-py2.7-macosx-10.6-intel.egg/twisted/internet/process.py", line 466, in _execChild\n os.execvpe(executable, args, environment)\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 353, in execvpe\n _execvpe(file, args, env)\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 368, in _execvpe\n func(file, *argrest)\nOSError: [Errno 2] No such file or directory\n']

I do not see any obvious reason why the code should file with a [Errno 2] No such file or directory\n'] error..

Tia

like image 961
aaberga Avatar asked Jun 16 '26 03:06

aaberga


1 Answers

The program you want to run is "tail". You want to pass it several arguments: "-n", "100", and "/var/log/system.log".

Instead, what your code does is run the program "tail -n 100 /var/log/system.log", which presumably does not exist on your system (I wouldn't expect it to).

The proper use of getProcessOutput is to pass the program separately from the argument list:

getProcessOutput("tail", ["-n", "100", "/var/log/system.log"])
like image 167
Jean-Paul Calderone Avatar answered Jun 20 '26 14:06

Jean-Paul Calderone