首页 >>  正文

f在python中表示什么

来源:baiyundou.net   日期:2024-08-22

【CSDN 编者按】IBM工程师Martin Heinz发文表示,Python即将迎来了真正的多线程时刻!

原文:https://martinheinz.dev/blog/97

未经授权,禁止转载!

作者 | Martin Heinz 责编 | 梦依丹

翻译工具 | ChatGPT

32岁的Python依然没有真正的并行性/并发性。然而,这种情况即将发生改变,因为在即将发布的Python 3.12中引入了名为"Per-Interpreter GIL"(全局解释器锁)的新特性。在距离发布还有几个月的时间(预计2023年10月发布),但相关代码已经有了,因此,我们可以提前了解如何使用子解释器API编写真正的并发Python代码。

子解释器

首先,让我们来解释一下如何通过“Per-Interpreter GIL”来解决Python缺乏适当的并发性问题。

在Python中,GIL是一个互斥锁,它只允许一个线程控制Python解释器。这意味着即使在Python中创建多个线程(例如使用线程模块),也只有一个线程会运行。

随着“Per-Interpreter GIL”的引入,各个Python解释器不再共享同一个GIL。这种隔离级别允许每个子解释器可以同时运行。这意味着我们可以通过生成额外的子解释器来绕过Python的并发限制,其中每个子解释器都有自己的GIL(全局状态)。

更详细的说明请参见PEP 684,该文档描述了此功能/更改:https://peps.python.org/pep-0684/#per-interpreter-state

上手体验

安装

为使用这项最新功能,我们必须安装最新版的Python,并且需要从源码上进行构建:

# https://devguide.python.org/getting-started/setup-building/#unix-compilinggit clone https://github.com/python/cpython.gitcd cpython./configure --enable-optimizations --prefix=$(pwd)/python-3.12make -s -j2./python# Python 3.12.0a7+ (heads/main:22f3425c3d, May 10 2023, 12:52:07) [GCC 11.3.0] on linux# Type "help", "copyright", "credits" or "license" for more information.

C-API在哪里?

既然已经安装了最新的版本,那我们该如何使用子解释器呢?可以直接导入吗?不,正如PEP-684中所提到的:“这是一个高级功能,专为C-API的一小部分用户设计。”

目前,Per-Interpreter GIL特性只能通过C-API使用,因此Python开发人员没有直接的接口可以使用。这样的接口预计将随着PEP 554一起推出,如果被采纳,则应该会在Python 3.13中实现,在那之前,我们必须自己想办法实现子解释器。

通过CPython代码库中的一些零散记录,我们可以采用下面两种方法:

使用_xxsubinterpreters模块,该模块是用C实现的,因此名称看起来有些奇怪。由于它是用C实现的,所以开发者无法轻易地检查代码(至少不是在 Python 中);

或者可以利用CPython的测试模块,该模块具有用于测试的示例 Interpreter(和 Channel)类。

# Choose one of these:import _xxsubinterpreters as interpretersfrom test.support import interpreters

在接下来的演示中,我们将主要采用第二种方法。我们已经找到了子解释器,但还需要从Python的测试模块中借用一些辅助函数,以便将代码传递给子解释器:

from textwrap import dedentimport os# https://github.com/python/cpython/blob/# 15665d896bae9c3d8b60bd7210ac1b7dc533b093/Lib/test/test__xxsubinterpreters.py#L75def _captured_script(script): r, w = os.pipe() indented = script.replace('\\n', '\\n ') wrapped = dedent(f""" import contextlib with open({w}, 'w', encoding="utf-8") as spipe: with contextlib.redirect_stdout(spipe): {indented} """) return wrapped, open(r, encoding="utf-8")def _run_output(interp, request, channels=None): script, rpipe = _captured_script(request) with rpipe: interp.run(script, channels=channels) return rpipe.read()

将interpreters模块与上述的辅助程序组合在一起,便可以生成第一个子解释器:

from test.support import interpretersmain = interpreters.get_main()print(f"Main interpreter ID: {main}")# Main interpreter ID: Interpreter(id=0, isolated=None)interp = interpreters.create()print(f"Sub-interpreter: {interp}")# Sub-interpreter: Interpreter(id=1, isolated=True)# https://github.com/python/cpython/blob/# 15665d896bae9c3d8b60bd7210ac1b7dc533b093/Lib/test/test__xxsubinterpreters.py#L236code = dedent(""" from test.support import interpreters cur = interpreters.get_current() print(cur.id) """)out = _run_output(interp, code)print(f"All Interpreters: {interpreters.list_all()}")# All Interpreters: [Interpreter(id=0, isolated=None), Interpreter(id=1, isolated=None)]print(f"Output: {out}") # Result of 'print(cur.id)'# Output: 1

生成和运行新解释器的一个方法是使用create函数,然后将解释器与要执行的代码一起传递给_run_output辅助函数。

更简单点的方法是:

interp = interpreters.create()interp.run(code)

使用解释器中的run方法。可是,如果我们运行上述代码中的任意一个,都会得到如下错误:

Fatal Python error: PyInterpreterState_Delete: remaining subinterpretersPython runtime state: finalizing (tstate=0x000055b5926bf398)

为避免此类错误发生,还需要清理一些悬挂的解释器:

def cleanup_interpreters(): for i in interpreters.list_all(): if i.id == 0: # main continue try: print(f"Cleaning up interpreter: {i}") i.close() except RuntimeError: pass # already destroyedcleanup_interpreters()# Cleaning up interpreter: Interpreter(id=1, isolated=None)# Cleaning up interpreter: Interpreter(id=2, isolated=None)

线程

虽然使用上述辅助函数运行代码是可行的,但使用线程模块中熟悉的接口可能更加方便:

import threadingdef run_in_thread(): t = threading.Thread(target=interpreters.create) print(t) t.start() print(t) t.join() print(t)run_in_thread()run_in_thread()# # # # # #

我们通过把interpreters.create函数传递给Thread,它会自动在线程内部生成新的子解释器。

我们也可以结合这两种方法,并将辅助函数传递给threading.Thread:

import timedef run_in_thread(): interp = interpreters.create(isolated=True) t = threading.Thread(target=_run_output, args=(interp, dedent(""" import _xxsubinterpreters as _interpreters cur = _interpreters.get_current() import time time.sleep(2) # Can't print from here, won't bubble-up to main interpreter assert isinstance(cur, _interpreters.InterpreterID) """))) print(f"Created Thread: {t}") t.start() return tt1 = run_in_thread()print(f"First running Thread: {t1}")t2 = run_in_thread()print(f"Second running Thread: {t2}")time.sleep(4) # Need to sleep to give Threads time to completecleanup_interpreters()

这里,我们演示了如何使用_xxsubinterpreters模块而不是test.support中的模块。我们还在每个线程中睡眠2秒钟来模拟一些“工作”。请注意,我们甚至不必调用join()函数等待线程完成,只需在线程完成时清理解释器即可。

Channels

如果我们深入研究CPython测试模块,我们还会发现有 RecvChannel 和 SendChannel 类的实现,它们类似于 Golang 中的通道(Channel)。要使用它们:

# https://github.com/python/cpython/blob/# 15665d896bae9c3d8b60bd7210ac1b7dc533b093/Lib/test/test_interpreters.py#L583r, s = interpreters.create_channel()print(f"Channel: {r}, {s}")# Channel: RecvChannel(id=0), SendChannel(id=0)orig = b'spam's.send_nowait(orig)obj = r.recv()print(f"Received: {obj}")# Received: b'spam'cleanup_interpreters()# Need clean up, otherwise:# free(): invalid pointer# Aborted (core dumped)

这个例子展示了如何创建一个带有receiver(r)和sender(s)端的通道。我们可以使用send_nowait将数据传递给发送方,并使用recv函数在另一侧读取它。这个通道实际上只是另一个子解释器 - 所以与之前一样 - 我们需要在完成后进行清理。

深入挖掘

最后,如果我们想要干扰或调整在C代码中设置的子解释器选项,那么可以使用test.support模块中的代码,具体来说就是run_in_subinterp_with_config:

import test.supportdef run_in_thread(script): test.support.run_in_subinterp_with_config( script, use_main_obmalloc=True, allow_fork=True, allow_exec=True, allow_threads=True, allow_daemon_threads=False, check_multi_interp_extensions=False, own_gil=True, )code = dedent(f""" from test.support import interpreters cur = interpreters.get_current() print(cur) """)run_in_thread(code)# Interpreter(id=7, isolated=None)run_in_thread(code)# Interpreter(id=8, isolated=None)

这个函数是一个Python API,用于调用C函数。它提供了一些子解释器选项,如own_gil,指定子解释器是否应该拥有自己的GIL。

总结

话虽如此——也正如你所看到的,API调用并不简单,除非你已具备C语言专业知识,并且又迫切想要使用字解释器,否则建议还是等待Python 3.13的发布。或者您可以尝试extrainterpreters项目,该项目提供更友好的Python API以便使用子解释器。

","gnid":"9956ec435c7bfa16a","img_data":[{"flag":2,"img":[{"desc":"","height":"80","s_url":"https://p0.ssl.img.360kuai.com/t013d73ffee4a20366b_1.gif","title":"","url":"https://p0.ssl.img.360kuai.com/t013d73ffee4a20366b.gif","width":"640"}]}],"original":0,"pat":"art_src_1,fts0,sts0","powerby":"cache","pub_time":1684491729000,"pure":"","rawurl":"http://zm.news.so.com/80752638082f9b466a7e6369cfcb10da","redirect":0,"rptid":"d162cb29895325f3","rss_ext":[],"s":"t","src":"CSDN","tag":[],"title":"真正的Python多线程来了!

麻受逄2844y=f(x) 其中" f " 代表什么 -
文妻牧17663002186 ______ f是运算法则,即对x进行的加,减,乘,除,乘方,开方等等运算.例如:y=f(x)=5x+1,f就表示对x乘5,再加1. 常数吗,就是一个确定的数,这个随着你的继续学习会知道的.例如,用a代表一个常数,你就可以将a看做一个确定的数,如,解方程x-1+a=0 如果a是常数,你就可以解为x=1-a.将a理解为确定的一个数.

麻受逄2844python2.7中打开文件失败f= open('C:\Users\Administrator\Desktop\12345.txt','r') -
文妻牧17663002186 ______ 因为在python和很多程序语言中"\"转义符号,要想输出\要么多加一个\写成...

麻受逄2844int和%在python语言中有什么作用?有哪些不同? -
文妻牧17663002186 ______ int在python里是一个类,表示是整形,比如1,2,3这些在python里都是int形. 你可以直接 a=1,这时候a就是int形的变量; 也可以 s=int(1), 这个和上面的效果是一样的. 在屏幕输出的命令中,%是格式符号,%d代表整数,%s代表字符 单独看%,是一个运算符号,求余数 1%5 = 1, 2%5 = 2, 3%5 = 3, 4%5 = 4, 5%5 = 0 另外一个简单的用途是,通过运算结果判断一个数是否能被另外一个数整除

麻受逄2844python中的for in是什么意思 -
文妻牧17663002186 ______ 1、python中的for in是一个语句循环固定格式; 2、Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言; 3、Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读、易维护,并且被大量用户所欢迎的、...

麻受逄2844f(X)中f代表什么 -
文妻牧17663002186 ______ f(x)是数学中的一种符号 是函数的一种表达方式 其中f表示法则的意思 例如 y=4x+5 则 f(x)=4x+5 f(x)组合只是用了这个符号而已

麻受逄2844""" """在python中是什么意思 -
文妻牧17663002186 ______ 一般在格式化的时候会用到%.这里你用的 %Y表示年,%m表示月,%d表示日 %H表示时,%M表示分,%S表示秒 单独用%号的时候,实际上没什么意义.

麻受逄2844如何在python%的编码URL参数 -
文妻牧17663002186 ______ Python中函数参数的传递是通过“赋值”来传递的,函数参数的接收传递有四种形式: 1. F(arg1,arg2,...) 2. F(arg2=,arg3=...) 3. F(*arg1) 4. F(**arg1) 第1 种方式是最“传统”的方式:一个函数可以定义不限个数参数,参数(形式参数)放在跟...

麻受逄2844为什么python 里def叫方法,这词什么来历? -
文妻牧17663002186 ______ 这种看翻译的了. def 是针对函数的关键字,若是在类里面的函数,可称为类的“方法”、“操作”,以对应于“属性”.其实在英文表达中就是同一个词.

麻受逄2844python 中 方法def total(*numbers, **keywords): 参数列表中1个*和2个*分别代表什么意识 -
文妻牧17663002186 ______ 用在函数定义里,numbers捕获了所有的位置参数(非keyword参数),keywords捕获了所有的keywords参数.例子:>>> def f(*args, **kwargs):. . . print type(args), args. . . print type(kwargs), kwargs. . . >>> f(1,2, a=3)<type 'tuple'> (1, 2)<type 'dict'...

(编辑:自媒体)
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图 @ 白云都 2024