Python多线程模型 [英] Python multithreading model

查看:62
本文介绍了Python多线程模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 python 中的多线程,但是我对一些问题感到困惑-

  1. 首先,python线程库创建的线程是用户级线程还是内核级线程?

书上说用户级线程必须映射到内核线程操作系统只创建和维护内核级线程.

python

  1. 有没有办法让操作系统在我的 Python 程序中遵循某种线程模型?

  2. 是否可以显示一个进程的所有正在运行的线程,并将它们的状态分别标记为内核级或用户级.还可以显示两个级别(用户和内核)之间的映射吗?

解决方案

通常,您永远不会直接创建内核级线程"——您在用户空间中所做的一切都将在用户空间中执行,否则即使是随机的浏览器 JavaScript 也会在内核级别保证在几秒钟内整个互联网都会变黑.

因此,在大多数语言中,线程接口(如果支持)与实际的内核线程"相距甚远,并且根据实现,它将链接到较低级别的线程接口(pthreads 例如)或者只是模拟用户不知道的线程.沿着这条链走下去,pthreads 可能会也可能不会链接到实际的内核"线程(这在 Linux 上是正确的,但在 Windows 上还有另一个层次的分离),但即便如此,代码还是会执行在用户空间 - 支持"内核线程用于控制代码单独运行的调度.

说到 CPython,它的threading 接口链接到 pthreads 因此,从技术上讲,存在一条从 Python 线程一直到内核线程的链.然而,Python 也有可怕的 GIL 几乎可以保证这一点,除了一些罕见的例外,主要与我有关/O,没有两个线程同时执行,这几乎使它的线程在协作多任务模式下运行.但是,由于在大多数系统上,进程也由内核线程支持,因此您仍然可以通过使用 multiprocessing 接口.

此外,在您的系统上有多个内核/CPU 之前,即使内核线程也以协作多任务模式执行,因此,从技术上讲,内核线程并不能保证您所描述的实际多线程.

关于如何列出线程及其依赖关系,可以使用top -H -p 来展示一个进程的线程树.

I have been studying multithreading in python for a while, however I was confused on a few issues-

  1. Firstly, are the threads created by the python threading library user level or kernel level threads?

Books say that user level threads must be mapped to kernel threads and the operating system only creates and maintains kernel level threads.

Which thread model will be used in the python threading library? Further, who makes the choice between kernel and user level threads? Is it the operating system or can the programmer have a say?

If the many-to-one model (illustrated in the picture) is used, I think it is not real multithreading, since all the threads map to a single kernel thread.

  1. Is there a way to direct the operating system to adhere to a certain threading model in my python program?

  2. Can all running threads for a process be shown with their state separately marked as either kernel or user level. Also can the mappings between the two levels (user and kernel) be shown?

解决方案

Usually, you never create 'kernel level threads' directly - everything you do in user space executes in user space, otherwise even a random browser JavaScript would be executing at the kernel level guaranteeing that within seconds the whole internet would go dark.

Thus, in most languages, a threading interface (if supported) is far removed from the actual 'kernel threads' and depending on implementation it will either link to a lower-level threading interface (pthreads for example) or just simulate threading unbeknownst to the user. Going down that chain, pthreads may or may not link to actual 'kernel' threads (it happens to be true on Linux, but on Windows there is another level of separation) but even then, the code executes in the user space - the 'supporting' kernel thread is there to control the scheduling the code runs separately.

When it comes to CPython, its threading interface links to pthreads so, technically, there is a chain from a Python thread all the way down to the kernel threads. However, Python also has the dreaded GIL pretty much guaranteeing that, with some rare exceptions mostly related to I/O, no two threads ever execute at the same time, pretty much making its threads operate in a cooperative multitasking mode. However, since on most systems processes are also backed by kernel threads, you can still utilize them in all their glory by using the multiprocessing interface.

Also, until you have multiple cores/CPUs on your system even kernel threads execute in a cooperative multitasking mode so, technically, kernel threads don't guarantee actual multi-threading as you're describing it.

As for how to list threads and their dependencies, you can use top -H -p <pid> to show the thread tree of a process.

这篇关于Python多线程模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆