carry out independent process

Asked

Viewed 404 times

5

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

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

Another need is to recover the PID from the process - for this reason I am not using the the system.().

I did tests with the the.spawnl(), but I was unsuccessful. Does anyone have any suggestions?

follows the code where I tested the the.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

2 answers

3


I was able to solve it this way:

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

0

  • Hello Michael, thanks for the comment, unfortunately this solution does not suit me for not returning the application’s PID

Browser other questions tagged

You are not signed in. Login or sign up in order to post.