随笔分类 -  Python

摘要:Py_Initialize 完成Python解释器的初始化 Py_FinalizeEx Py_Initialize的逆操作,释放所有Python申请的资源。有时Python不是以独立进程运行的,而是被动态加载到其它进程里面,此时用该函数就可以释放Python的资源而不影响主进程的执行。Python独 阅读全文
posted @ 2022-03-26 09:06 鸪斑兔 阅读(1604) 评论(0) 推荐(0) 编辑
摘要:Python解释器的资源的申请和释放都由主线程完成 全局变量_PyRuntimeState中有一个字段_finalizing就是标记python解释器是否正在退出,这个字段在解释器初始化时被设置为null 当主线程要退出时(在Py_RunMain中调用Py_FinalizeEx,会开始主线程的退出流 阅读全文
posted @ 2022-03-25 23:47 鸪斑兔 阅读(220) 评论(0) 推荐(0) 编辑
摘要:https://rushter.com/blog/python-gil-thread-scheduling/ import threading from types import SimpleNamespace DEFAULT_INTERVAL = 0.05 gil_mutex = threadin 阅读全文
posted @ 2022-03-24 15:00 鸪斑兔 阅读(40) 评论(0) 推荐(0) 编辑
摘要:申请GIL static void take_gil(PyThreadState *tstate) { ... // 获取mutex,然后就可以安全地访问GIL了 MUTEX_LOCK(gil->mutex); // 不断检查GIL状态,一直等到GIL被释放 while (_Py_atomic_lo 阅读全文
posted @ 2022-03-21 22:41 鸪斑兔 阅读(414) 评论(0) 推荐(0) 编辑
摘要:1、GIL的作用:保证任意时刻仅有一个线程在执行,防止多线程并发执行导致的数据异常。 2、操作系统的pthread_mutex_t也能达到线程互斥的作用,为什么不用? 注:Linux 环境中,实现线程同步的常用方法有 4 种,分别称为 互斥锁 、 信号量 、 条件变量 和 读写锁 。 互斥锁 (Mu 阅读全文
posted @ 2022-03-17 22:47 鸪斑兔 阅读(49) 评论(0) 推荐(0) 编辑
摘要:PyCodeObject:代码对象,就是一段代码编译后形成的对象,函数中对应的就是函数体的代码编译结果。 PyFunctionObject :函数对象,它是对PyCodeObject的封装,相当于 PyCodeObject + 函数def定义这一行代码。它在PyCodeObject基础上增加了函数的 阅读全文
posted @ 2022-02-20 23:25 鸪斑兔 阅读(626) 评论(0) 推荐(0) 编辑
摘要:https://flaggo.github.io/python3-source-code-analysis/ https://fasionchan.com/python-source/ https://he11olx.com/tags/CPython3-6%E6%BA%90%E7%A0%81/ ht 阅读全文
posted @ 2021-11-17 11:43 鸪斑兔 阅读(272) 评论(0) 推荐(0) 编辑
摘要:NDEBUG:关闭DEBUG信息。在release版本必须在Python.h中显示定义该宏变量,否则代码中的assert语句不会被移除(会影响性能。) 阅读全文
posted @ 2021-11-17 09:56 鸪斑兔 阅读(26) 评论(0) 推荐(0) 编辑
摘要:原因:IDEA将工程误认为了Java工程。 解决方法:如果确认这是个Python工程,修改*.iml文件,将下面的这一行删除 阅读全文
posted @ 2019-10-23 10:07 鸪斑兔 阅读(1043) 评论(0) 推荐(0) 编辑
摘要:运行效果: 阅读全文
posted @ 2018-08-31 11:00 鸪斑兔 阅读(1118) 评论(0) 推荐(0) 编辑
摘要:def mock_open(data): from StringIO import StringIO stmp = StringIO(data) return stmp _open = __builtins__.open __builtins__.open = mock_open # test case here __builtins__.open = _open 阅读全文
posted @ 2017-03-20 15:51 鸪斑兔 阅读(314) 评论(0) 推荐(0) 编辑
摘要:import anydbm as dbm import cPickle as pickle class BottleBucket(object): '''Memory-caching wrapper around anydbm''' def __init__(self, name): self.__dict__['name'] = name se... 阅读全文
posted @ 2017-02-23 16:17 鸪斑兔 阅读(168) 评论(0) 推荐(0) 编辑
摘要:line是模板中一行的内容,类似: {{x}}testinfo{{x+10}} x=10时,模板输出: 10testinfo20 阅读全文
posted @ 2017-02-22 20:28 鸪斑兔 阅读(435) 评论(0) 推荐(0) 编辑
摘要:>>> hobbies = ['software'] >>> ('notchecked', 'checked')['software' in hobbies] 'checked' >>> 'checked' if 'software' in hobbies else 'notchecked' 'checked' >>> hobbies = ['xxx'] >& 阅读全文
posted @ 2017-02-10 16:36 鸪斑兔 阅读(145) 评论(0) 推荐(0) 编辑
摘要:一个带有参数的装饰器的例子: bottle.py中自动转换参数类型的装饰器: 阅读全文
posted @ 2017-02-06 10:24 鸪斑兔 阅读(1808) 评论(0) 推荐(0) 编辑
摘要:import re class SimpleTemplate(object): re_block = re.compile(r'^\s*%\s*((if|elif|else|try|except|finally|for|while|with).*:)\s*$') re_end = re.compile(r'^\s*%\s*end(.*?)\s*$') re_cod... 阅读全文
posted @ 2017-02-04 10:35 鸪斑兔 阅读(247) 评论(0) 推荐(0) 编辑
摘要:# Now search regexp routes # ROUTES_REGEXP是一个字典,键是请求方法,值是[路由, 处理函数]的列表 # 例如:{"GET", [[路由1, 处理函数1], [路由2, 处理函数2]]} routes = ROUTES_REGEXP.get(method,[]) for i in xrange(len(routes)): match = route... 阅读全文
posted @ 2017-01-26 11:14 鸪斑兔 阅读(246) 评论(0) 推荐(0) 编辑
摘要:# Routing def compile_route(route): """ Compiles a route string and returns a precompiled RegexObject. Routes may contain regular expressions with named groups to support url parameters. ... 阅读全文
posted @ 2017-01-25 17:35 鸪斑兔 阅读(569) 评论(0) 推荐(0) 编辑
摘要:class Test(object): def __get__(self, instance, type=None): """ 只让类访问, 而不让类的实例来访问。 具体是靠 __get__(self, instance, type=None) 方法来实现来的: 第二个参数 instance, 当 class.attr 的时... 阅读全文
posted @ 2016-11-28 09:47 鸪斑兔 阅读(240) 评论(0) 推荐(0) 编辑
摘要:# -*- coding: utf8 -*-import logging# 创建一个loggerlogger = logging.getLogger('mylogger')logger.setLevel(logging.DEBUG)# 创建一个handler,用于写入日志文件fh = logging... 阅读全文
posted @ 2014-09-16 09:16 鸪斑兔 阅读(1273) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示