cpython

Is a variable swap guaranteed to be atomic in python?

谁说胖子不能爱 提交于 2019-12-17 10:56:26
问题 With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following: (x, y) = (y, x) will be guaranteed atomic in cPython. (x and y are both python variables) 回答1: Let's see: >>> x = 1 >>> y = 2 >>> def swap_xy(): ... global x, y ... (x, y) = (y, x) ... >>> dis.dis(swap_xy) 3 0 LOAD_GLOBAL 0 (y) 3 LOAD_GLOBAL 1 (x) 6 ROT_TWO 7 STORE_GLOBAL 1 (x) 10 STORE_GLOBAL 0 (y) 13 LOAD_CONST 0 (None) 16

CPython memory allocation

此生再无相见时 提交于 2019-12-17 10:33:51
问题 This is a post inspired from this comment about how memory is allocated for objects in CPython. Originally, this was in the context of creating a list and appending to it in a for loop vis a vis a list comprehension. So here are my questions: how many different allocaters are there in CPython? what is the function of each? when is malloc acutally called? (a list comprehension may not result in a call to malloc , based on what's said in this comment How much memory does python allocate for

__builtin__.iterator does not exist?

夙愿已清 提交于 2019-12-13 17:32:46
问题 Consider: Python 2.7.3 (default, Aug 2 2012, 16:55:39) [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import __builtin__ >>> type(iter('123')) <type 'iterator'> >>> type(iter('123')).__class__ <type 'type'> >>> type(iter('123')).__name__ 'iterator' >>> type(iter('123')).__module__ '__builtin__' >>> __builtin__.iterator Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module'

python线程

不问归期 提交于 2019-12-13 17:19:22
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> python线程 一、什么是线程 进程是资源分配的最小单位,线程则是CPU调度的最小单位。 线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 特点: 易于调度。 提高并发性。通过线程可方便有效地实现并发性。进程可创建多个线程来执行同一程序的不同部分。 开销少。创建线程比创建进程要快,所需开销很少。 利于充分发挥多处理器的功能。通过创建多线程进程,每个线程在一个处理器上运行,从而实现应用程序的并发性,使每个处理器都得到充分运行。 二、GIL(Global Interpreter Lock)全局解释器锁 Python代码的执行由Python 虚拟机(也叫解释器主循环,CPython版本)来控制,Python 在设计之初就考虑到要在解释器的主循环中,同时只有一个线程在执行,即在任意时刻,只有一个线程在解释器中运行。对Python 虚拟机的访问由全局解释器锁(GIL)来控制,正是这个锁能保证同一时刻只有一个线程在运行。 在多线程环境中,Python 虚拟机按以下方式执行: 设置GIL 切换到一个线程去运行 运行: a. 指定数量的字节码指令,或者 b. 线程主动让出控制(可以调用time

Kill hanging function in Python in multithreaded enviorment

一个人想着一个人 提交于 2019-12-13 13:18:44
问题 I would like to kill a function that executes to long. What is important this function is inside C extension (wrapped in Cython) , and I would like this solution to work in multithreaded enviorment. Since it is wrapped in Cython this thread could hold GIL. I have no control whatsoever on what is happening inside this extension (and I think that this code will not respond to interrupts). I'm fairly certain that this code will be only run on Unix machines. But question Python kill hanging

Are there any datetime.tzinfo implementations in C?

為{幸葍}努か 提交于 2019-12-13 08:14:35
问题 I've been working on a Python library that uses a C extension module to do ISO 8601 parsing. Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Calls out to Python implementations of tzinfo (currently pytz.FixedOffset ) are simply too slow. In Python 3.7, datetime.timezone is finally exposed to the C-API. My code takes adavantage of it, and gets a tremendous performance boost from using a C implementation instead of a Python one. I'd love

Is there a way to run cpython on a diffident thread without risking a crash?

风格不统一 提交于 2019-12-13 03:37:55
问题 I have a program that runs lots of urllib requests IN AN INFINITE LOOP, which makes my program really slow, so I tried putting them as threads. Urllib uses cpython deep down in the socket module, so the threads that are being created just add up and do nothing because python's GIL prevents a two cpython commands from being executed in diffident threads at the same time. I am running Windows XP with Python 2.5, so I can't use the multiprocess module. I tried looking at the subproccess module

Backward compatibility of Python 3.5 for external modules

…衆ロ難τιáo~ 提交于 2019-12-12 19:16:52
问题 I have built a Python C++ module based on Python 3.4.3. Later I have installed Python 3.5, and tried to load my Python module. When I tried to do so, I got the following error: ImportError: Module use of python34.dll conflicts with this version of Python. I try to import my module by running Python from the command prompt. So I wonder: Is there no backward compatibility in Python 3.5 for modules that were built with previous versions? Must I build my module again with Python 3.5? 回答1: By

The optimal way to set a breakpoint in the Python source code while debugging CPython by GDB

只愿长相守 提交于 2019-12-12 19:11:33
问题 I use GDB to understanding how CPython executes the test.py source file and I want to stop the CPython when it starts the execution of opcode I am interested. OS: Ubuntu 18.04.2 LTS Debugger: GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git The first problem - many CPython's .py own files are executed before my test.py gets its turn, so I can't just break at the _PyEval_EvalFrameDefault - there are many of them, so I should distinguish my file from others. The second problem - I can't set the

What does __flags__ in python type used for

感情迁移 提交于 2019-12-12 18:06:21
问题 I have been read pickle source code recently. The following code in copy_reg make me confused: _HEAPTYPE = 1<<9 def _reduce_ex(self, proto): assert proto < 2 for base in self.__class__.__mro__: if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE: break else: base = object # not really reachable if base is object: state = None So what does __flags__ using for? I found it is defined in type object: type.__flags__ = 2148423147 I was tried to search it in the official doc, but