gpt4 book ai didi

python - Python 上下文中的运行时是什么?它由什么组成?

转载 作者:行者123 更新时间:2023-12-03 23:35:08 26 4
gpt4 key购买 nike

在这个问题的上下文中 What is “runtime”? (https://stackoverflow.com/questions/3900549/what-is-runtime/3900561)

我试图了解 python 运行时由什么组成。我的猜测是:

  • 包含所有运行时变量的 python 进程。
  • GIL
  • 底层解释器代码(CPython 等)。

  • 现在,如果这是正确的,我们是否可以说 python 中的多处理创建了多个运行时并且一个 python 进程是我们可以直接与运行时相关的东西? (我认为这是正确的选择)

    或者,每个 python 线程都有自己的堆栈,与父进程在相同的 GIL 和内存空间上工作,可以被称为具有单独的运行时?

    或者,不管有多少线程或进程正在运行,它都会在一个运行时下运行?

    简单来说,Python 上下文中运行时的定义是什么?

    PS:我理解线程和进程之间的区别。 GIL:我理解这些影响,但我不理解。

    最佳答案

    你在谈论计算机科学中两个不同(但相似)的概念;多进程和多线程。以下是一些可能有用的问题/答案汇编:

  • Multiprocessing -- Wikipedia

  • Multiprocessing is the use of two or more central processing units (CPUs) within a single computer system.The term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between them.


  • Multithreading -- Wikipedia

  • In computer architecture, multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system. This approach differs from multiprocessing. In a multithreaded application, the threads share the resources of a single or multiple cores, which include the computing units, the CPU caches, and the translation lookaside buffer (TLB).


  • What is the difference between a process and a thread? -- StackOverflow

  • Process

    Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

    Thread

    A thread is an entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.


  • Meaning of “Runtime Environment” and of “Software framework”? -- StackOverflow

  • A runtime environment basically is a virtual machine that runs on top of a machine - provides machine abstraction. It is generally lower level than a library. A framework can contain a runtime environment, but is generally tied to a library.


  • Runtime System -- Wikipedia

  • In computer programming, a runtime system, also called runtime environment, primarily implements portions of an execution model. Most languages have some form of runtime system that provides an environment in which programs run. This environment may address a number of issues including the layout of application memory, how the program accesses variables, mechanisms for passing parameters between procedures, interfacing with the operating system, and otherwise. Typically the runtime system will have some responsibility for setting up and managing the stack and heap, and may include features such as garbage collection, threads or other dynamic features built into the language.


  • global interpreter lock -- Python Docs

  • The mechanism used by the CPython interpreter to assure that only one thread executes Python bytecode at a time. This simplifies the CPython implementation by making the object model (including critical built-in types such as dict) implicitly safe against concurrent access. Locking the entire interpreter makes it easier for the interpreter to be multi-threaded, at the expense of much of the parallelism afforded by multi-processor machines.

    However, some extension modules, either standard or third-party, are designed so as to release the GIL when doing computationally-intensive tasks such as compression or hashing. Also, the GIL is always released when doing I/O.

    Past efforts to create a “free-threaded” interpreter (one which locks shared data at a much finer granularity) have not been successful because performance suffered in the common single-processor case. It is believed that overcoming this performance issue would make the implementation much more complicated and therefore costlier to maintain.


  • What is the Python Global Interpreter Lock (GIL)?-- Real Python

  • 有关 GIL 的更多信息的有用来源。
  • Does python os.fork uses the same python interpreter? -- StackOverflow

  • Whenever you fork, the entire Python process is duplicated in memory (including the Python interpreter, your code and any libraries, current stack etc.) to create a second process - one reason why forking a process is much more expensive than creating a thread.

    This creates a new copy of the python interpreter.

    One advantage of having two python interpreters running is that you now have two GIL's (Global Interpreter Locks), and therefore can have true multi-processing on a multi-core system.

    Threads in one process share the same GIL, meaning only one runs at a given moment, giving only the illusion of parallelism.


  • Memory Management -- Python Docs

  • Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.



    当您通过 threading 库生成线程时,您实际上是在单个 Python 运行时内生成作业。此运行时确保线程具有共享内存并通过 global interpreter lock 管理这些线程的运行顺序:
  • Understanding the Python GIL -- dabeaz

  • 当您通过 multiprocessing 库生成进程时,您生成的新进程包含运行指定代码的新 Python 解释器(新运行时)。如果你想共享内存,你必须使用 multiprocessing.shared_memory :
  • multiprocessing.shared_memory -- Python Docs

  • This module provides a class, SharedMemory, for the allocation and management of shared memory to be accessed by one or more processes on a multicore or symmetric multiprocessor (SMP) machine. To assist with the life-cycle management of shared memory especially across distinct processes, a BaseManager subclass, SharedMemoryManager, is also provided in the multiprocessing.managers module.



    我们可以说python中的多处理创建了多个运行时并且一个python进程是我们可以直接与运行时相关的东西吗?
    是的。不同的 GIL,不同的内存空间,不同的运行时。
    每个 python 线程都有自己的堆栈,与父进程在相同的 GIL 和内存空间上工作,可以被称为具有单独的运行时吗?
    取决于您所说的“堆栈”是什么意思。相同的 GIL,共享内存空间,相同的运行时间。
    不管有多少线程和进程正在运行,它都会在一个运行时下运行?
    取决于是否多线程/多进程。
    简单来说,Python 上下文中运行时的定义是什么?
    运行时环境实际上是 python.exe/usr/bin/python 。它是 Python 可执行文件,它将通过将 Python 代码转换为 CPU 可读的字节码来解释它。当您使用多线程时,您只会运行一个 python。当您 multiprocess 时,您有多个 python 正在运行。

    我希望核心开发人员可以进来并更详细地对此进行更多讨论。目前,以上只是一个来源的汇编,供您开始理解/看到更大的图景。

    关于python - Python 上下文中的运行时是什么?它由什么组成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60273813/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com