load independent process

5

I'm having trouble loading processes from a python application.

Before I was using subprocess.Popen but it creates subprocesses of the main application and in my case, I need to create processes that run independently, in the sense that, if my application falls, the processes do not fall.

Another need is to retrieve the process PID (for this reason I am not using os.system ()).

I tested os.spawnl () but did not succeed. Anyone have any suggestions?

Follow the code where I tested os.spawnl ()

>>> import os
>>> programa = r'C:\Program Files (x86)\mongoDB\bin\mongod.exe'
>>> parametros = r'--logpath "C:\Foo\Bar\Base\install.log" --dbpath "C:\Foo\Bar\Base\data\db" --port 1124'
>>> os.path.dirname(programa)
'C:\Program Files (x86)\mongoDB\bin'
>>> os.path.basename(programa)
'mongod.exe'
>>> os.spawnl(os.P_WAIT, os.path.dirname(programa), os.path.basename(programa), parametros)
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    os.spawnl(os.P_WAIT, os.path.dirname(programa), os.path.basename(programa), parametros)
  File "C:\Python33\lib\os.py", line 922, in spawnl
    return spawnv(mode, file, args)
>>> os.spawnl(os.P_WAIT, "%s %s".format(programa, parametros))
#aqui ocorre crash no pythonw
    
asked by anonymous 24.06.2014 / 15:16

2 answers

3

I was able to resolve it as follows:

import platform
import subprocess

def carregar_processo(cmd):
    if platform.system() == "Windows":
        DETACHED_PROCESS = 0x00000008
        CREATE_NEW_PROCESS_GROUP = 0x00000200
        return subprocess.Popen(cmd, creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP).pid
    else:
        return subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).pid
    
25.06.2014 / 20:07
0

On Windows, you can use the os.startfile function. So:

import os
os.startfile('C:\Windows\Notepad.exe')

If you need a platform-independent solution, take a look at this link .

    
24.06.2014 / 19:23