简体   繁体   中英

Why is Python's subprocess' popen so different between unix and windows?

I am trying to write cross-platform code in Python. The code should be spawning new shells and run code.

This lead me to look at Python's subprocess tool and in particular its Popen part. So I read through the documentation for this class Popen doc and find too many "if on Unix/if on Windows" statements. Not very cross-platform, unless I have misunderstood the doc.

What is going on? I understand that the two operating systems are different, but really, there is no way to write a common interface? I mean, the same arguments "windows is different than unix" can be applied to os , system , etc., and they all seem 100 % cross-platform.

The problem is that process management is something deeply engrained in the operating system and differs greatly not only in the implementation but often even in the basic functionality. It's actually often rather easy to abstract code in for example the os class. Both C libraries, be it *nix or Windows, implement reading files as an I/O stream, so you can even write rather low level file operation functions which work the same in Windows and *nix.

But processes differ greatly. In *nix for example processes are all hierarchical, every process has a parent and all processes go back to the init system running under PID 1. A new process gets created by forking itself, checking if it's the parent or the child and then continuing accordingly. In Windows processes are strictly non-hierarchical and get created by the CreateProcess () system call, for which you need special privileges. There a good deal more differences, these were just two examples, but I hope it shows that implementing a platform independent process library is a very daunting task.

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