Python:何时使用pty.fork()和os.fork() [英] Python: when to use pty.fork() versus os.fork()

查看:325
本文介绍了Python:何时使用pty.fork()和os.fork()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的应用程序生成外部后台进程时,我不确定是使用pty.fork()还是os.fork(). (例如国际象棋引擎)

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

我希望如果父进程被杀死,则生成的进程会消失,就像终端中生成应用程序一样.

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?

推荐答案

使用os.fork()创建的子进程从父进程继承stdin/stdout/stderr,而使用pty.fork()创建的子进程连接到新的伪终端.在编写类似xterm之类的程序时,需要稍后使用:父进程中的pty.fork()返回一个描述符以控制子进程的终端,因此您可以直观地表示来自其的数据,并将用户操作转换为终端输入序列.

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.

更新:

在pty(7)手册页中:

From pty(7) man page:

一个有望被连接的过程 到终端,可以打开从机端 伪终端,然后是 由具有 打开主机端.任何东西 写在主端是 提供给从站上的进程 就像输入输入一样结束 一个终端.例如,编写 中断字符(通常是 控制C)到主设备 会引起中断信号 为(SIGINT)生成 前台进程组 连接到奴隶.反过来, 任何写到 伪终端的从端可以 被以下过程读取 连接到主端.

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.

这篇关于Python:何时使用pty.fork()和os.fork()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆