0

I'm trying to spawn a subprocess that should still be running after the main process closed. This part works fine, but if I redirect the output of this process to a file, I can't start the script a second time because the process still blocks the log file.

This short example demonstrates the problem: In this case the second process is "notepad" and is started by "other.cmd". While the main process/script is "test_it.py" which is started by "start_it.cmd".

start_it.cmd

@python test_it.py > test.log

test_it.py

import subprocess
from subprocess import DEVNULL, STDOUT
subprocess.Popen(["other.cmd"], stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT)

other.cmd

start notepad

When start_it.cmd is executed the second time, it will fail with this error message "The process cannot access the file because it is being used by another process".

How can I start the subprocess so that it doesn't block the log file?

spmdc
  • 338
  • 3
  • 13
  • That's a very Windows-specific problem, not a Python issue as such -- on UNIX platforms, multiple programs can have the same file open for write at the same time without needing to jump through any hoops. Making that clear in the title will help getting folks in here whose focus is on the language itself, or on platforms where the issue doesn't exist or apply. – Charles Duffy Apr 15 '19 at 14:37
  • possible duplicate? https://stackoverflow.com/questions/11196367/processing-single-file-from-multiple-processes – nickyfot Apr 15 '19 at 14:38
  • @nickthefreak I don't see how any of that is related to this problem. – spmdc Apr 15 '19 at 14:42
  • the redirection "locks" the file, as long as the redirecting program is running. But you are able to `copy` or `type` the contents so far. – Stephan Apr 15 '19 at 14:44
  • The error message is probably: `The process cannot access the file because it is being used by another process.`. If you are already using Python in the process, why not using a decent log module. If you insist on doing it from cmd, the only solution I see here is with a Pipe and a custom application that will open the output file in shared (nonblock) mode. – Pagefault Apr 15 '19 at 14:57

2 Answers2

1

A solution using a pipe. multiplexer.py

with open('log.txt', 'a') as outputFile:
    while True:
        data = sys.stdin.read(1024)
        if None == data:
            break
        outputFile.write(data)

start_it.cmd

@python test_it.py | python multiplexer.py

Everything else stays the same.

Pagefault
  • 339
  • 1
  • 12
0

I found a solution that is close to what I originally intended:

subprocess.Popen("explorer other.cmd", shell=True)

By letting the explorer start the .cmd file this succesfully detaches the called .cmd from the original process. And thus doesn't keep the log file open.

spmdc
  • 338
  • 3
  • 13