Googling on starting background processes in Python does not shed any light yet. If anyone knows, please share your ideas.
#MAYA PYTHON LS FOR UNUSED NODES CODE#
I have not checked the code on other platforms and do not know the reasons of the behaviour on FreeBSD. And the working solution was the following: pid = subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) Some experiments showed that the problem seemed to be in sharing sys.stdout. And that is not what you want in a CGI script either. On FreeBSD we have another problem: when the parent process is finished, it finishes the child processes as well.
* UPD 2015.10.27 in a comment below notes, that the semantically correct flag is CREATE_NEW_CONSOLE (0x00000010) */ If you happen to have installed pywin32, you can import the flag from the win32process module, otherwise you should define it yourself: DETACHED_PROCESS = 0x00000008
#MAYA PYTHON LS FOR UNUSED NODES WINDOWS#
The solution is to pass DETACHED_PROCESS Process Creation Flag to the underlying CreateProcess function in Windows API. The problem is not specific to Python in the PHP community the problems are the same. On Windows (Windows XP), the parent process will not finish until the longtask.py has finished its work. My target platform was FreeBSD, but the development was on Windows, so I faced the problem on Windows first. But it is not clear what happens after the line 'some more code here' from the example. The idea here is that you do not want to wait in the line 'call subprocess' until the longtask.py is finished. Pid = subprocess.Popen() # Call subprocess The classical example from the subprocess module documentation is: import subprocess That is, the child process should live longer than the CGI script execution process. Suppose you want to start a long task from a CGI script. Some hints on detaching the child process from the calling one (starting the child process in background). To give you a hint of the implications consider this code: print subprocess.Popen("echo %s " % user_input, stdout=PIPE).stdout.read()Īnd imagine that the user enters something " my mama didnt love me & rm -rf /" which could erase the whole filesystem. If you are unsure, only use these methods with constants. For example, if a user is entering some/any part of the string. There are serious security implications if any part of the string that you pass can not be fully trusted. The subprocess module should probably be what you use.įinally, please be aware that for all methods where you pass the final command to be executed by the shell as a string and you are responsible for escaping it. Os.fork, os.exec, os.spawn are similar to their C language counterparts, but I don't recommend using them directly. Similar to the above but even more flexible and returns a CompletedProcess object when the command finishes executing. For example: return_code = subprocess.call("echo Hello World", shell=True) This is basically just like the Popen class and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. Instead of print os.popen("echo Hello World").read()īut it is nice to have all of the options there in one unified class instead of 4 different popen functions. For example, you'd say: print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read() This is intended as a replacement for os.popen, but has the downside of being slightly more complicated by virtue of being so comprehensive. If you pass everything as a string, then your command is passed to the shell if you pass them as a list then you don't need to worry about escaping anything. There are 3 other variants of popen that all handle the i/o slightly differently. Os.popen will do the same thing as os.system except that it gives you a file-like object that you can use to access standard input/output for that process. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs. However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, et cetera. For example: os.system("some_command output_file") This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. Os.system passes the command and arguments to your system's shell. Summary of ways to call external programs, including their advantages and disadvantages: