Python: when to use pty.fork() versus os.fork()

前端 未结 3 1150
别跟我提以往
别跟我提以往 2021-02-05 14:52

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 t

3条回答
  •  独厮守ぢ
    2021-02-05 15:09

    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.

    Update:

    From pty(7) man page:

    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.

提交回复
热议问题