0

I have the following code that starts a process, which copies files. However, it does not wait for process to finish, any help would be appreciated!

p = Popen([batch_command, numberID, date_string, wait_string, environment],  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, errors = p.communicate()
p.wait() # wait for process to terminate

I have tried using p.communicate at the end instead of p.wait(), however it didn't seem to work. I need for program to wait until all subprocesses are completed.

I tried the following code so that the process can wait, however, it did not really help.

while p.poll() is None: 
    time.sleep(0.5)                     
log.write("Process is running...")

Attempting to do it with win32api as a ctype, I came up with the following python script to implement a waiting time until the process is completed. I want it to wait until its completed, I do not want to kill it.

if p.returncode is None:
    if win32event.WaitForSingleObject(p._handle, 0) == win32event.WAIT_OBJECT_0:
                p.returncode = GetExitCodeProcess(p._handle)
return p.returncode
AlpU
  • 363
  • 1
  • 9
  • 26
  • related: [Python subprocess .check_call vs .check_output](http://stackoverflow.com/q/36169571/4279) (make sure you read it and understood—ask if you have any questions). Though if you are on Windows; it might not be directly applicable. – jfs Jun 06 '16 at 18:12
  • both `.communicate()` and `.wait()` wait for the child process to exit. It is pointless to call it more than once (unless there are `threading` bugs). The process is reaped if `.communicate()` returns – jfs Jun 06 '16 at 18:14
  • Thank you a lot @J.F.Sebastian. I just read your explanation in your first comment. As far as I understand, if I can use p.check_output() will that do the same thing and wait? Do you think putting it on loop, maybe not with p.wait() but instead with p.poll() would work? – AlpU Jun 06 '16 at 18:34
  • 1
    no, no, and no. If `p.communicate()` doesn't work for you then `check_output()` won't work either (it uses `.communicate()` under the hood). Again, it is pointless to call `.wait()` after `.communicate()`: the latter already calls the former internally. It is pointless to call any of the mentioned functions in a loop¶ Your issue is likely that your `batch_command` creates multiple processes (as it is said in the link I've provided: the parent process may exit *before* its children). If you want to wait for all processes then how to do it depends on your OS, what are the specific processes, etc – jfs Jun 06 '16 at 18:49
  • I am using Windows to do this. What would you recommend to wait for the processes to finish? @J.F.Sebastian – AlpU Jun 06 '16 at 20:02
  • you could do the opposite: you could kill the descendant process once the top-most parent exits. See [Python: how to kill child process(es) when parent dies?](http://stackoverflow.com/q/23434842/4279) – jfs Jun 06 '16 at 22:12
  • I am trying for all of the descendant and top-most parent's process to be completed before script ends and quits. That is why, I want the program to wait until everything is done. @J.F.Sebastian – AlpU Jun 07 '16 at 17:22
  • related: [How do I wait until all processes in a job have exited?](https://blogs.msdn.microsoft.com/oldnewthing/20130405-00/?p=4743) – jfs Jun 07 '16 at 17:28
  • I read the link and it mentions about Wait­For­Single­Object in C++, I checked the sample code given as well. I am unsure if there is a version for Python as well though. @J.F.Sebastian – AlpU Jun 07 '16 at 19:49
  • you have to translate it from C to Python using ctypes or win32api modules (in a similar way as it shown in the first link) – jfs Jun 07 '16 at 20:08
  • I just edited my question by including win32api as well and the way to convert it to python. I would appreciate if you can help me out to construct that script @J.F.Sebastian – AlpU Jun 07 '16 at 21:40
  • you should call `GetQueuedCompletionStatus()` (as shown in the last link), not `WaitForSingleObject()` – jfs Jun 07 '16 at 21:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114141/discussion-between-pythoner1234-and-j-f-sebastian). – AlpU Jun 08 '16 at 14:48

0 Answers0