Python:何时使用pty.fork()与os.fork()

时间:2022-10-24 21:05:51

I'm uncertain whether to use pty.fork() or os.fork() when spawning external background processes from my app. (Such as chess engines)

我不确定在从我的应用生成外部后台进程时是使用pty.fork()还是os.fork()。

I want the spawned processes to die if the parent is killed, as with spawning apps in a terminal.

如果父进程被杀死,我希望生成的进程终止,就像在终端中生成应用程序一样。

What are the ups and downs between the two forks?

这两个分叉之间的起起伏伏是什么?

3 个解决方案

#1


10  

The child process created with os.fork() inherits stdin/stdout/stderr from parent process, while the child created with pty.fork() is connected to new pseudo terminal. You need the later when you write a program like xterm: pty.fork() in parent process returns a descriptor to control terminal of child process, so you can visually represent data from it and translate user actions into terminal input sequences.

fork()创建的子进程从父进程继承stdin/stdout/stderr,而使用pty.fork()创建的子进程则连接到新的pseudo终端。当您在父进程中编写xterm: pti .fork()之类的程序时,您需要后面的代码来返回一个描述符,以控制子进程的终端,这样您就可以可视化地表示数据,并将用户操作转换为终端输入序列。

Update:

更新:

From pty(7) man page:

从企业(7)手册页:

A process that expects to be connected to a terminal, can open the slave end of a pseudo-terminal and then be driven by a program that has opened the master end. Anything that is written on the master end is provided to the process on the slave end as though it was input typed on a terminal. For example, writing the interrupt character (usually control-C) to the master device would cause an interrupt signal (SIGINT) to be generated for the foreground process group that is connected to the slave. Conversely, anything that is written to the slave end of the pseudo-terminal can be read by the process that is connected to the master end.

希望连接到终端的进程可以打开伪终端的从端,然后由打开主端的程序驱动。在主端上写入的任何内容都被提供给从端上的进程,就好像它是在终端上输入的一样。例如,将中断字符(通常是control-C)写入主设备将导致为连接到从设备的前台进程组生成中断信号(SIGINT)。相反,任何写到伪终端的从端的内容都可以被连接到主端的进程读取。

#2


3  

In the past I've always used the subprocess module for this. It provides a good api for communicating with subprocesses.

在过去,我一直使用子过程模块。它为与子进程通信提供了良好的api。

You can use call(*popenargs, **kwargs) for blocking execution of them, and I believe using the Popen class can handle async execution.

您可以使用调用(*popenargs, **kwargs)来阻止它们的执行,我相信使用Popen类可以处理异步执行。

Check out the docs for more info.

更多信息请查看文档。

As far as using os.fork vs pty.fork, both are highly platform dependent, and neither will work (or at least is tested) with windows. The pty module seems to be the more constrained of the two by reading the docs. The main difference being the pseudo terminal aspect. So if you aren't willing to architect your code in such a way as to be able to use the subprocess module, I'd probably go with os.fork instead of pty.fork.

至于使用操作系统。叉vs企业。fork,两者都是高度依赖于平台的,在windows上都不能用(或者至少是经过测试)。通过阅读文档,pty模块似乎更受这两个模块的约束。主要的区别在于伪终端方面。因此,如果您不愿意以能够使用子过程模块的方式来构建代码,我可能会使用os。叉代替pty.fork。

#3


2  

Pseudotermials are necessary for some applications that really expect a terminal. An interactive shell is one of these examples but there are many other. The pty.fork option is not there as another os.fork but as a specific API to use a pseudoterminal.

对于一些真正需要终端的应用程序来说,伪termials是必需的。交互式shell就是其中的一个例子,但是还有很多其他的例子。企业。fork选项不作为另一个操作系统。但作为使用伪终端的特定API。

#1


10  

The child process created with os.fork() inherits stdin/stdout/stderr from parent process, while the child created with pty.fork() is connected to new pseudo terminal. You need the later when you write a program like xterm: pty.fork() in parent process returns a descriptor to control terminal of child process, so you can visually represent data from it and translate user actions into terminal input sequences.

fork()创建的子进程从父进程继承stdin/stdout/stderr,而使用pty.fork()创建的子进程则连接到新的pseudo终端。当您在父进程中编写xterm: pti .fork()之类的程序时,您需要后面的代码来返回一个描述符,以控制子进程的终端,这样您就可以可视化地表示数据,并将用户操作转换为终端输入序列。

Update:

更新:

From pty(7) man page:

从企业(7)手册页:

A process that expects to be connected to a terminal, can open the slave end of a pseudo-terminal and then be driven by a program that has opened the master end. Anything that is written on the master end is provided to the process on the slave end as though it was input typed on a terminal. For example, writing the interrupt character (usually control-C) to the master device would cause an interrupt signal (SIGINT) to be generated for the foreground process group that is connected to the slave. Conversely, anything that is written to the slave end of the pseudo-terminal can be read by the process that is connected to the master end.

希望连接到终端的进程可以打开伪终端的从端,然后由打开主端的程序驱动。在主端上写入的任何内容都被提供给从端上的进程,就好像它是在终端上输入的一样。例如,将中断字符(通常是control-C)写入主设备将导致为连接到从设备的前台进程组生成中断信号(SIGINT)。相反,任何写到伪终端的从端的内容都可以被连接到主端的进程读取。

#2


3  

In the past I've always used the subprocess module for this. It provides a good api for communicating with subprocesses.

在过去,我一直使用子过程模块。它为与子进程通信提供了良好的api。

You can use call(*popenargs, **kwargs) for blocking execution of them, and I believe using the Popen class can handle async execution.

您可以使用调用(*popenargs, **kwargs)来阻止它们的执行,我相信使用Popen类可以处理异步执行。

Check out the docs for more info.

更多信息请查看文档。

As far as using os.fork vs pty.fork, both are highly platform dependent, and neither will work (or at least is tested) with windows. The pty module seems to be the more constrained of the two by reading the docs. The main difference being the pseudo terminal aspect. So if you aren't willing to architect your code in such a way as to be able to use the subprocess module, I'd probably go with os.fork instead of pty.fork.

至于使用操作系统。叉vs企业。fork,两者都是高度依赖于平台的,在windows上都不能用(或者至少是经过测试)。通过阅读文档,pty模块似乎更受这两个模块的约束。主要的区别在于伪终端方面。因此,如果您不愿意以能够使用子过程模块的方式来构建代码,我可能会使用os。叉代替pty.fork。

#3


2  

Pseudotermials are necessary for some applications that really expect a terminal. An interactive shell is one of these examples but there are many other. The pty.fork option is not there as another os.fork but as a specific API to use a pseudoterminal.

对于一些真正需要终端的应用程序来说,伪termials是必需的。交互式shell就是其中的一个例子,但是还有很多其他的例子。企业。fork选项不作为另一个操作系统。但作为使用伪终端的特定API。