简体   繁体   中英

what is the state of a thread lock in python after callingl subprocess.popen

Having worked out painfully that there is a race hazard in a multi-threaded program between opening a file and setting the 'close on exec' bit in one thread and calling subprocess.Popen in another thread - which can result in unexpected handles being passed to the 2nd child, it seems to me I need to protect this access with a lock (I know closing all the handles is possible from subprocess.Popen but that might be overkill).

Is that going to be safe? The subprocess is going to exec a shell immediately but I'm not sure how python threading locks behave in that sort of situation.

PS I know linux has a 'close on exec' bit for open, but I'm not running on linux, and anyway, the python tempfile (or at least the 2.6 one) doesn't use that facility.

Ideally of course, python would deal with that nastiness itself, but I can't find anything suggesting it might.

It sounds quite safe. If you do

with my_exec_lock:
    open_file()
    set_coe()

in one thread and

with my_exec_lock:
    popen()

in the other, you should be safe.

But be aware that whis way, the 1st thread might be blocked until popen() is finished.

Maybe one of the other Threading mechanisms could be more appropriate.

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