0

I am trying to launch and then stop an Android emulator from a python 2.5 script (to be executed with monkeyrunner), and I wrote the following code:

def launchDevice(avd_name, wipe=False):
    cmd = ['~/android-sdk-linux/tools/emulator -avd %s' % avd_name]
    if wipe:
        cmd.append(' -wipe-data')

    print 'Starting the emulator %s...' % avd_name
    emulator = subprocess.Popen(cmd, shell=True, bufsize=-1)
    return emulator

#end

def main():
    # launch the emulator
    emulator_proc = launchDevice('Nexus5')
    print emulator_proc.pid
    # stop the emulator
    os.kill(emulator_proc.pid, signal.SIGTERM)

#end

if __name__ == '__main__':
    main()

When I execute this script I get a working Android emulator (so the subprocess.Popen instruction works), but then I get an error (TypeError: kill(): 1st arg can't be coerced to int) in os.kill function since the emulator_proc's PID is None (verified by the output message).

How is it possible to have a None PID?

user2340612
  • 10,053
  • 4
  • 41
  • 66
  • 1
    Puzzling... it shouldn't be None. The `Popen` constructor initializes it to `None` but by the time `__init__` is done, its set to the child pid. All failures in `__init__` raise an exception but in that case, `emulator_proc` would not have been assigned. – tdelaney Nov 21 '15 at 17:30
  • Is this the whole code? If not, maybe some other component is inadvertently resetting `emulator_proc.pid` to None. Studying the [2.5 version of `subprocess.Popen`](https://hg.python.org/cpython/file/2.5/Lib/subprocess.py) doesn't reveal an obvious way for process launch to succeed with the `pid` field remaining None - the line `self.pid = os.fork()` ties the success of forking to storing the PID. – user4815162342 Nov 21 '15 at 17:45
  • 1
    Yes, that's all the code (the only part missing are the `import`s). By the way I executed that code inside a python shell and discovered that it works; so I inspected the `monkeyrunner` script and found that actually it is a Java program that exploits Jython, so probably the problem is a bug in Jython's implementation of Popen or something like that – user2340612 Nov 21 '15 at 18:04

0 Answers0