Does python os.fork uses the same python interpreter?

后端 未结 3 1191
死守一世寂寞
死守一世寂寞 2021-02-05 17:17

I understand that threads in Python use the same instance of Python interpreter. My question is it the same with process created by os.fork? Or does each process cr

3条回答
  •  轮回少年
    2021-02-05 17:56

    os.fork() is equivalent to the fork() syscall in many UNIC(es). So yes your sub-process(es) will be separate from the parent and have a different interpreter (as such).

    man fork:

    FORK(2)

    NAME fork - create a child process

    SYNOPSIS #include

       pid_t fork(void);
    

    DESCRIPTION fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:

    pydoc os.fork():

    os.fork() Fork a child process. Return 0 in the child and the child’s process id in the parent. If an error occurs OSError is raised.

    Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have known issues when using fork() from a thread.

    See also: Martin Konecny's response as to the why's and advantages of "forking" :)

    For brevity; other approaches to concurrency which don't involve a separate process and therefore a separate Python interpreter include:

    • Green or Lightweight threads; ala greenlet
    • Coroutines ala Python generators and the new Python 3+ yield from
    • Async I/O ala asyncio, Twisted, circuits, etc.

提交回复
热议问题