简体   繁体   中英

Can't write to file from child process

I can't wrap my head around this... I have the following code:

def launch(command):
    pf = os.path.join(working_directory, '.pid')

    pid = os.fork()
    if pid == 0:
        stdout = os.open(..., os.O_WRONLY | os.O_CREAT)
        try:
            proc = Popen(command, shell=False, stdout=stdout, cwd=workdir)
            print(proc.pid)
            with open(pf, 'wb') as p: # pf should not be open as the file is just created.
                p.write(proc.pid)
            print("Hello World")
        except OSError as proc_error:
            ...
        finally:
            os._exit(o) # socketserver catches SystemExit exception (flask)
    else:
        start = time.time()
        while not os.path.isfile(pf): # I'm just checking if that file exists here never opened it in the first place.
            if time.time() - start >= 30:
                 raise TimeoutError...
            time.sleep(5)

        pid = int(open(pf, 'rb').read())

Here's the output:

  • $pid
  • TimeoutError occurred

The script seem to be hanging at opening pf for writing. I verified, the file if not created, Hello World never gets printed.

Why is this happening, and how can fix it?

Thanks!

I have reduced your code to this (removing everything I could not reproduce given your code):

import os
import time
s = "foo"
pid = os.fork()
from subprocess import Popen

if pid == 0:
    proc = Popen(["sleep", "3"])
    with open(s, "w") as p:
        p.write(str(proc.pid)) # <- Only real error I could see
    os._exit(0)

else:
    start = time.time()
    while not os.path.isfile(s):
        if time.time() - start >= 30:
            raise TimeoutError("Command took to long")
        time.sleep(5)

    print("Read from file: " + open(s, 'r').read())

However, it works just fine, it prints Read from file: 12075 . So the issue is not in the part that can be reproduced, given your code.

To read/write the procid to the binary file I succesfully used the pickle module:

pickle.dump(proc.pid,p) # write to file
pickle.load(open(s, "rb")) #read from file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM