Python运行时环境

news2024/11/13 16:37:05

1.1概念

程序运行只有栈帧不够,还要有进程与线程

Python在初始化时会创建一个主线程,所以其运行时环境中存在一个主线程,可以有多个线程

Python实现了对多线程的支持,而且一个线程就是操作系统上的一个原生线程

Python中所有线程都使用虚拟机(软CPU)来完成计算工作

某一个PyFrameObject就是执行环境,一整个为执行环境。

注:线程切换之前要保存关于当前线程的信息。

1.2 线程状态信息的抽象

通过PyThreadState对象实现,所以可以将该对象视为线程本身的抽象

typedef struct _ts PyThreadState;
struct _ts {

    PyThreadState *prev;
    PyThreadState *next;
    PyInterpreterState *interp;

    struct {
        /* Has been initialized to a safe state.

           In order to be effective, this must be set to 0 during or right
           after allocation. */
        unsigned int initialized:1;

        /* Has been bound to an OS thread. */
        unsigned int bound:1;
        /* Has been unbound from its OS thread. */
        unsigned int unbound:1;
        /* Has been bound aa current for the GILState API. */
        unsigned int bound_gilstate:1;
        /* Currently in use (maybe holds the GIL). */
        unsigned int active:1;

        /* various stages of finalization */
        unsigned int finalizing:1;
        unsigned int cleared:1;
        unsigned int finalized:1;

        /* padding to align to 4 bytes */
        unsigned int :24;
    } _status;

    int py_recursion_remaining;
    int py_recursion_limit;

    int c_recursion_remaining;
    int recursion_headroom; /* Allow 50 more calls to handle any errors. */

    /* 'tracing' keeps track of the execution depth when tracing/profiling.
       This is to prevent the actual trace/profile code from being recorded in
       the trace/profile. */
    int tracing;
    int what_event; /* The event currently being monitored, if any. */

    /* Pointer to current _PyCFrame in the C stack frame of the currently,
     * or most recently, executing _PyEval_EvalFrameDefault. */
    _PyCFrame *cframe;

    Py_tracefunc c_profilefunc;
    Py_tracefunc c_tracefunc;
    PyObject *c_profileobj;
    PyObject *c_traceobj;

    /* The exception currently being raised */
    PyObject *current_exception;

    /* Pointer to the top of the exception stack for the exceptions
     * we may be currently handling.  (See _PyErr_StackItem above.)
     * This is never NULL. */
    _PyErr_StackItem *exc_info;

    PyObject *dict;  /* Stores per-thread state */

    int gilstate_counter;

    PyObject *async_exc; /* Asynchronous exception to raise */
    unsigned long thread_id; /* Thread id where this tstate was created */

    /* Native thread id where this tstate was created. This will be 0 except on
     * those platforms that have the notion of native thread id, for which the
     * macro PY_HAVE_THREAD_NATIVE_ID is then defined.
     */
    unsigned long native_thread_id;

    struct _py_trashcan trash;

    /* Called when a thread state is deleted normally, but not when it
     * is destroyed after fork().
     * Pain:  to prevent rare but fatal shutdown errors (issue 18808),
     * Thread.join() must wait for the join'ed thread's tstate to be unlinked
     * from the tstate chain.  That happens at the end of a thread's life,
     * in pystate.c.
     * The obvious way doesn't quite work:  create a lock which the tstate
     * unlinking code releases, and have Thread.join() wait to acquire that
     * lock.  The problem is that we _are_ at the end of the thread's life:
     * if the thread holds the last reference to the lock, decref'ing the
     * lock will delete the lock, and that may trigger arbitrary Python code
     * if there's a weakref, with a callback, to the lock.  But by this time
     * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
     * of C code can be allowed to run (in particular it must not be possible to
     * release the GIL).
     * So instead of holding the lock directly, the tstate holds a weakref to
     * the lock:  that's the value of on_delete_data below.  Decref'ing a
     * weakref is harmless.
     * on_delete points to _threadmodule.c's static release_sentinel() function.
     * After the tstate is unlinked, release_sentinel is called with the
     * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
     * the indirectly held lock.
     */
    void (*on_delete)(void *);
    void *on_delete_data;

    int coroutine_origin_tracking_depth;

    PyObject *async_gen_firstiter;
    PyObject *async_gen_finalizer;

    PyObject *context;
    uint64_t context_ver;

    /* Unique thread state id. */
    uint64_t id;

    _PyStackChunk *datastack_chunk;
    PyObject **datastack_top;
    PyObject **datastack_limit;
    /* XXX signal handlers should also be here */

    /* The following fields are here to avoid allocation during init.
       The data is exposed through PyThreadState pointer fields.
       These fields should not be accessed directly outside of init.
       This is indicated by an underscore prefix on the field names.

       All other PyInterpreterState pointer fields are populated when
       needed and default to NULL.
       */
       // Note some fields do not have a leading underscore for backward
       // compatibility.  See https://bugs.python.org/issue45953#msg412046.

    /* The thread's exception stack entry.  (Always the last entry.) */
    _PyErr_StackItem exc_state;

    /* The bottom-most frame on the stack. */
    _PyCFrame root_cframe;
};

1.3 进程的抽象

允许有多个进程(Interpreter)存在,但通常只有一个Interpreter,这个Interpreter维护了一个或多个PyThreadState对象。

通过PyInterpreterState对象实现

typedef struct _is PyInterpreterState;.
struct _is {

    PyInterpreterState *next;

    int64_t id;
    int64_t id_refcount;
    int requires_idref;
    PyThread_type_lock id_mutex;

    /* Has been initialized to a safe state.

       In order to be effective, this must be set to 0 during or right
       after allocation. */
    int _initialized;
    int finalizing;

    uint64_t monitoring_version;
    uint64_t last_restart_version;
    struct pythreads {
        uint64_t next_unique_id;
        /* The linked list of threads, newest first. */
        PyThreadState *head;
        /* Used in Modules/_threadmodule.c. */
        long count;
        /* Support for runtime thread stack size tuning.
           A value of 0 means using the platform's default stack size
           or the size specified by the THREAD_STACK_SIZE macro. */
        /* Used in Python/thread.c. */
        size_t stacksize;
    } threads;

    /* Reference to the _PyRuntime global variable. This field exists
       to not have to pass runtime in addition to tstate to a function.
       Get runtime from tstate: tstate->interp->runtime. */
    struct pyruntimestate *runtime;

    /* Set by Py_EndInterpreter().

       Use _PyInterpreterState_GetFinalizing()
       and _PyInterpreterState_SetFinalizing()
       to access it, don't access it directly. */
    _Py_atomic_address _finalizing;

    struct _gc_runtime_state gc;

    /* The following fields are here to avoid allocation during init.
       The data is exposed through PyInterpreterState pointer fields.
       These fields should not be accessed directly outside of init.

       All other PyInterpreterState pointer fields are populated when
       needed and default to NULL.

       For now there are some exceptions to that rule, which require
       allocation during init.  These will be addressed on a case-by-case
       basis.  Also see _PyRuntimeState regarding the various mutex fields.
       */

    // Dictionary of the sys module
    PyObject *sysdict;

    // Dictionary of the builtins module
    PyObject *builtins;

    struct _ceval_state ceval;

    struct _import_state imports;

    /* The per-interpreter GIL, which might not be used. */
    struct _gil_runtime_state _gil;

     /* ---------- IMPORTANT ---------------------------
     The fields above this line are declared as early as
     possible to facilitate out-of-process observability
     tools. */

    PyObject *codec_search_path;
    PyObject *codec_search_cache;
    PyObject *codec_error_registry;
    int codecs_initialized;

    PyConfig config;
    unsigned long feature_flags;

    PyObject *dict;  /* Stores per-interpreter state */

    PyObject *sysdict_copy;
    PyObject *builtins_copy;
    // Initialized to _PyEval_EvalFrameDefault().
    _PyFrameEvalFunction eval_frame;

    PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
    // One bit is set for each non-NULL entry in func_watchers
    uint8_t active_func_watchers;

    Py_ssize_t co_extra_user_count;
    freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];

#ifdef HAVE_FORK
    PyObject *before_forkers;
    PyObject *after_forkers_parent;
    PyObject *after_forkers_child;
#endif

    struct _warnings_runtime_state warnings;
    struct atexit_state atexit;

    struct _obmalloc_state obmalloc;

    PyObject *audit_hooks;
    PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
    PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
    // One bit is set for each non-NULL entry in code_watchers
    uint8_t active_code_watchers;

    struct _py_object_state object_state;
    struct _Py_unicode_state unicode;
    struct _Py_float_state float_state;
    struct _Py_long_state long_state;
    struct _dtoa_state dtoa;
    struct _py_func_state func_state;
    /* Using a cache is very effective since typically only a single slice is
       created and then deleted again. */
    PySliceObject *slice_cache;

    struct _Py_tuple_state tuple;
    struct _Py_list_state list;
    struct _Py_dict_state dict_state;
    struct _Py_async_gen_state async_gen;
    struct _Py_context_state context;
    struct _Py_exc_state exc_state;

    struct ast_state ast;
    struct types_state types;
    struct callable_cache callable_cache;
    PyCodeObject *interpreter_trampoline;

    _Py_GlobalMonitors monitors;
    bool f_opcode_trace_set;
    bool sys_profile_initialized;
    bool sys_trace_initialized;
    Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
    Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
    PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
    PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];

    struct _Py_interp_cached_objects cached_objects;
    struct _Py_interp_static_objects static_objects;

    // XXX Remove this field once we have a tp_* slot.
    struct _xidregistry xidregistry;
    /* The thread currently executing in the __main__ module, if any. */
    PyThreadState *threads_main;
    /* The ID of the OS thread in which we are finalizing.
       We use _Py_atomic_address instead of adding a new _Py_atomic_ulong. */
    _Py_atomic_address _finalizing_id;

   /* the initial PyInterpreterState.threads.head */
    PyThreadState _initial_thread;
};

1.4 建立新的PyFrameObject对象

PyFrameObject*
PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
            PyObject *globals, PyObject *locals)
{
    PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
    if (builtins == NULL) {
        return NULL;
    }
    PyFrameConstructor desc = {
        .fc_globals = globals,
        .fc_builtins = builtins,
        .fc_name = code->co_name,
        .fc_qualname = code->co_name,
        .fc_code = (PyObject *)code,
        .fc_defaults = NULL,
        .fc_kwdefaults = NULL,
        .fc_closure = NULL
    };
    PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
    if (func == NULL) {
        return NULL;
    }
    PyFrameObject *f = _PyFrame_New_NoTrack(code);
    if (f == NULL) {
        Py_DECREF(func);
        return NULL;
    }
    init_frame((_PyInterpreterFrame *)f->_f_frame_data, func, locals);
    f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
    f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
    // This frame needs to be "complete", so pretend that the first RESUME ran:
    f->f_frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable;
    assert(!_PyFrame_IsIncomplete(f->f_frame));
    Py_DECREF(func);
    _PyObject_GC_TRACK(f);
    return f;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2086487.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

#Datawhale X 李宏毅苹果书 AI夏令营#1.2了解线性模型

1.2线性模型 什么是线性模型? 初始模型:, 其中y表示观看人数,x1表示前一天的观看人数,这个模型就是在用前一天的观看人数来预测当前的观看人数。 模型改进: 然而真实的数据是有周期性的,每隔7天&#…

向量数据库Milvus源码开发贡献实践

Milvus 是一款云原生的开源向量数据库,广泛应用于高维向量数据的管理和人工智能驱动的相似性搜索。无论是在构建智能搜索引擎还是开发数据驱动的应用,Milvus 都能提供强大的支持。我们将一起从头开始实践 Milvus 的标准开发流程,包括如何搭建…

企业画册在线版是怎么制作的?

随着互联网技术的飞速发展,传统的纸质企业画册已经逐渐无法满足现代企业的需求。为了让画册更加环保、便捷,同时提升企业形象和品牌影响力,企业画册在线版应运而生。那么,企业画册在线版究竟是如何制作出来的呢?今天&a…

嵌入式学习(数据库)

数据库的定义: 可以理解为数据库是用来存放数据的一个容器。有了数据库后,我们可以直接查找数据。或者可以对数据库进行读写删除等操作。 Sqlite 小型数据库 . Sqlite特点: 可以实现大数据量的管理 读写速度慢 最常见的数据库类型是关系型数据库管理…

QLU-AI助手初次微调Qwen2-7B-Instruct总结

一、微调代码 from datasets import Dataset import pandas as pd from transformers import (AutoTokenizer,AutoModelForCausalLM,DataCollatorForSeq2Seq,TrainingArguments,Trainer,GenerationConfig) import torch from peft import LoraConfig, TaskType, get_peft_mode…

探索待办事项管理新世界:10款工具带你告别杂乱无章

国内外主流的10款免费待办事项app对比:1.PingCode;2.Worktile;3.Todoist;4.Trello;5.Habitica;6.Forest;7.Teambition;8.Asana;9.嘀嗒清单(TickTick&#xff…

poe供电原理以及应用

1,根据IEEE802.3af标准,一个完整的PoE系统包括供电端设备PSE和受电端设备PD两部分; 供电设备PSE是整个系统的电源提供者,为PD设备提供直流电源,其可分为M

[动态规划]---背包问题

前言 作者:小蜗牛向前冲 专栏:小蜗牛算法之路 专栏介绍:"蜗牛之道,攀登大厂高峰,让我们携手学习算法。在这个专栏中,将涵盖动态规划、贪心算法、回溯等高阶技巧,不定期为你奉上基础数据结构…

如何让ChatGPT说话更像人类

ChatGPT在多个领域展现了卓越的能力,但对话中仍不可避免地带有一定的机械感。幸运的是,OpenAI推出的GPTs功能可以让用户自定义prompt。最近,我发现了其中一个GPTs,它能让ChatGPT的对话更加贴近真人,现在就来与大家分享…

【生日视频制作】农村文化墙广告标语AE模板修改文字软件生成器教程特效素材【AE模板】

生日视频制作教程农村文化墙广告标语AE模板修改文字软件生成器 怎么如何做的【生日视频制作】农村文化墙广告标语AE模板修改文字软件生成器教程特效素材【AE模板】 生日视频制作步骤: 安装AE软件 下载AE模板 把AE模板导入AE软件 修改图片或文字 渲染出视频

复杂网络|节点重要性评价指标

author: xiao黄 time: 2024-08-28 公众号: 复杂网络与网络科学 CSDN: https://blog.csdn.net/Python_Matlab评价节点重要性算法的指标有多种,如基于信息传播方面的动力学模型、单调性、Distinct Metric以及基于网络脆弱性和鲁棒性的方法等。不同的评价指标所考虑的角…

Java Web_00001

目录 Web项目介绍网页的组成部分 HTMLHTML简介HTML示例HTML文件的书写规范HTML标签标签介绍标签的语法:常用标签font特殊字符标题标签超链接列表标签img标签表格标签跨行跨列表格iframe框架标签(内嵌窗口)表单标签表单的显示表单格式化表单提交细节 其他标签 CSSCSS…

羟基“消失术”,化学合成中的巧妙方法

羟基(-OH)是一个很常见的官能团,在有机合成的转化过程中往往起到桥梁作用。在化合物合成过程中由于合成选择的原因通常会先引入一些基团,以降低合成化合物的难度以及提高其产率。而羟基的引入或者生成是比较常见的。羟基化方法有很多,其中包括…

JDBC和Mybatis中的批处理

src目录下创建jdbc.properties mysql驱动5.1.6之后,只需要配置url,username,password mysql 5.1.6之后可以无需Class.forName("com.mysql.jdbc.Driver") * 从jdk1.5之后可以通过配置文件来配置 * 会自动加载mysql驱动jar包下META-INF/services/java.sql.Driver文本中…

Python深度学习股价预测、量化交易策略:LSTM、GRU深度门控循环神经网络|附代码数据

全文链接:https://tecdat.cn/?p37539 原文出处:拓端数据部落公众号 分析师:Shuo Zhang 本文以上证综指近 22 年的日交易数据为样本,构建深度门控循环神经网络模型,从股价预测和制定交易策略两方面入手&#xff0c…

8月29日

思维导图 作业&#xff1a; 仿照string类&#xff0c;实现myString 代码 mystring.h #ifndef MYSTRING_H #define MYSTRING_H#include <iostream> #include<string.h>using namespace std;class myString { private:char *str;int size;public://无参构造myStr…

在自己的数据集上测试coco评价指标——以Mar20为例

参考&#xff1a; 1.在自己的数据集上调用cocoapi计算map 2. COCO Result Format 3.COCO result json 之前的模型都是在COCO数据集上训练&#xff0c;数据集的标注以及结果的生成格式都是按照官方的格式组织的&#xff0c;调用cocoapi和官方下载的instance_val2017.json计算就可…

Win11 操作(七)声音降噪

前言 为了听脚步和不外放声音影响到女朋友休息&#xff0c;于是买了S21头戴式耳机&#xff0c;虽然目的都达到了&#xff0c;但是又有新问题出现 损害队友听力 由于天气炎热&#xff0c;家里都开着风扇&#xff0c;但是耳机没有降噪功能所以我的麦噪音极大&#xff0c;这就导…

OpenLayers3, 设置地图背景

文章目录 一、前言二、代码实现三、总结 一、前言 本文基于OpenLayers3&#xff0c;实现地图加入背景图的功能。 二、代码实现 <!DOCTYPE html> <html xmlns"http://www.w3.org/1999/xhtml"> <head><meta http-equiv"Content-Type"…

封装string

仿照string类&#xff0c;实现mystring #include <iostream> #include<string.h> using namespace std;class MyString {private:char *str; //记录c风格的字符串int size0; //记录字符串实际长度public://无参构造&#xff1a;定义了一个字符串MyStri…