草庐IT

python - 我的 Python 进程在哪些 CPU 内核上运行?

coder 2023-05-22 原文

设置

我用 Python(在 Windows PC 上)编写了一个相当复杂的软件。我的软件基本上启动了两个 Python 解释器 shell。当您双击 main.py 文件时,第一个 shell 启动(我想)。在该 shell 中,其他线程以下列方式启动:

    # Start TCP_thread
    TCP_thread = threading.Thread(name = 'TCP_loop', target = TCP_loop, args = (TCPsock,))
    TCP_thread.start()

    # Start UDP_thread
    UDP_thread = threading.Thread(name = 'UDP_loop', target = UDP_loop, args = (UDPsock,))
    TCP_thread.start()

Main_thread 启动一个 TCP_thread 和一个 UDP_thread。尽管它们是单独的线程,但它们都在一个 Python shell 中运行。

Main_thread也启动一个子进程。这是通过以下方式完成的:

p = subprocess.Popen(['python', mySubprocessPath], shell=True)

从 Python 文档中,我了解到此子进程在单独的 Python 解释器 session /shell 中同时 (!) 运行。此子进程中的 Main_thread 完全专用于我的 GUI。 GUI 为其所有通信启动一个 TCP_thread

我知道事情变得有些复杂。因此,我在此图中总结了整个设置:


我有几个关于此设置的问题。我会在这里列出它们:

问题 1 [已解决]

Python 解释器是否一次只使用一个 CPU 内核来运行所有线程?换句话说,Python 解释器 session 1(从图中)是否会运行所有 3 个线程(Main_threadTCP_threadUDP_thread) 在一个 CPU 内核上?

回答:是的,这是真的。 GIL(全局解释器锁)确保所有线程一次在一个 CPU 内核上运行。

问题 2 [尚未解决]

我有办法跟踪它是哪个 CPU 内核吗?

问题 3 [部分解决]

对于这个问题,我们忘记了 threads,但我们关注 Python 中的 subprocess 机制。启动一个新的子进程意味着启动一个新的 Python 解释器instance。这是正确的吗?

回答:是的,这是正确的。起初对于以下代码是否会创建新的 Python 解释器实例存在一些混淆:

    p = subprocess.Popen(['python', mySubprocessPath], shell = True)

问题已得到澄清。这段代码确实启动了一个新的 Python 解释器实例。

Python 是否足够聪明,可以让单独的 Python 解释器实例在不同的 CPU 内核上运行?有没有办法跟踪哪一个,也许还有一些零星的打印语句?

问题 4 [新问题]

社区讨论提出了一个新问题。生成新进程时(在新的 Python 解释器实例中)显然有两种方法:

    # Approach 1(a)
    p = subprocess.Popen(['python', mySubprocessPath], shell = True)

    # Approach 1(b) (J.F. Sebastian)
    p = subprocess.Popen([sys.executable, mySubprocessPath])

    # Approach 2
    p = multiprocessing.Process(target=foo, args=(q,))

第二种方法有一个明显的缺点,它只针对一个函数——而我需要打开一个新的 Python 脚本。无论如何,这两种方法在实现的目标上是否相似?

最佳答案

Q: Is it true that a Python interpreter uses only one CPU core at a time to run all the threads?

没有。 GIL 和 CPU 亲和性是不相关的概念。 GIL 可以在阻塞 I/O 操作期间释放,无论如何在 C 扩展中进行长时间的 CPU 密集型计算。

如果一个线程在 GIL 上被阻塞;它可能不在任何 CPU 内核上,因此可以公平地说纯 Python 多线程代码在 CPython 实现中一次只能使用一个 CPU 内核。

Q: In other words, will the Python interpreter session 1 (from the figure) run all 3 threads (Main_thread, TCP_thread and UDP_thread) on one CPU core?

我认为 CPython 不会隐式管理 CPU 关联性。它可能依赖于操作系统调度程序来选择在哪里运行线程。 Python 线程是在真正的 OS 线程之上实现的。

Q: Or is the Python interpreter able to spread them over multiple cores?

要找出可用 CPU 的数量:

>>> import os
>>> len(os.sched_getaffinity(0))
16

同样,线程是否调度在不同的 CPU 上并不依赖于 Python 解释器。

Q: Suppose that the answer to Question 1 is 'multiple cores', do I have a way to track on which core each thread is running, perhaps with some sporadic print statements? If the answer to Question 1 is 'only one core', do I have a way to track which one it is?

我想,一个特定的 CPU 可能会从一个时隙更改为另一个时隙。你可以 look at something like /proc/<pid>/task/<tid>/status on old Linux kernels .在我的机器上, task_cpu can be read from /proc/<pid>/stat or /proc/<pid>/task/<tid>/stat :

>>> open("/proc/{pid}/stat".format(pid=os.getpid()), 'rb').read().split()[-14]
'4'

对于当前的可移植解决方案,请查看 psutil 公开此类信息。

您可以将当前进程限制为一组 CPU:

os.sched_setaffinity(0, {0}) # current process on 0-th core

Q: For this question we forget about threads, but we focus on the subprocess mechanism in Python. Starting a new subprocess implies starting up a new Python interpreter session/shell. Is this correct?

是的。 subprocess模块创建新的操作系统进程。如果你运行 python可执行文件,然后它会启动一个新的 Python 解释器。如果您运行 bash 脚本,则不会创建新的 Python 解释器,即运行 bash可执行文件不会启动新的 Python 解释器/ session /等。

Q: Supposing that it is correct, will Python be smart enough to make that separate interpreter session run on a different CPU core? Is there a way to track this, perhaps with some sporadic print statements as well?

见上文(即,操作系统决定在哪里运行您的线程,并且可能有操作系统 API 公开线程的运行位置)。

multiprocessing.Process(target=foo, args=(q,)).start()

multiprocessing.Process还会创建一个新的操作系统进程(运行新的 Python 解释器)。

In reality, my subprocess is another file. So this example won't work for me.

Python 使用模块来组织代码。如果您的代码在 another_file.py然后 import another_file在您的主模块中并通过 another_file.foomultiprocessing.Process .

Nevertheless, how would you compare it to p = subprocess.Popen(..)? Does it matter if I start the new process (or should I say 'python interpreter instance') with subprocess.Popen(..)versus multiprocessing.Process(..)?

multiprocessing.Process()可能在 subprocess.Popen() 之上实现. multiprocessing提供类似于 threading 的 API API 并抽象出 Python 进程之间的通信细节(Python 对象如何序列化以在进程之间发送)。

如果没有 CPU 密集型任务,那么您可以在单个进程中运行您的 GUI 和 I/O 线程。如果您有一系列 CPU 密集型任务,那么要一次使用多个 CPU,请使用具有 C 扩展名的多个线程,例如 lxml , regex , numpy (或您自己使用 Cython 创建的)可以在长时间计算期间释放 GIL 或将它们卸载到单独的进程中(一种简单的方法是使用由 concurrent.futures 提供的进程池)。

Q: The community discussion raised a new question. There are apparently two approaches when spawning a new process (within a new Python interpreter instance):

# Approach 1(a)
p = subprocess.Popen(['python', mySubprocessPath], shell = True)

# Approach 1(b) (J.F. Sebastian)
p = subprocess.Popen([sys.executable, mySubprocessPath])

# Approach 2
p = multiprocessing.Process(target=foo, args=(q,))

“方法 1(a)” 在 POSIX 上是错误的(尽管它可能在 Windows 上工作)。为了便于携带,请使用“方法 1(b)”,除非您知道自己需要 cmd.exe (在这种情况下传递一个字符串,以确保使用正确的命令行转义)。

The second approach has the obvious downside that it targets just a function - whereas I need to open up a new Python script. Anyway, are both approaches similar in what they achieve?

subprocess创建新进程,any 进程,例如,您可以运行 bash 脚本。 multprocessing用于在另一个进程中运行 Python 代码。 导入 Python 模块并运行其功能比将其作为脚本运行更灵活。见 Call python script with input with in a python script using subprocess .

关于python - 我的 Python 进程在哪些 CPU 内核上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36795086/

有关python - 我的 Python 进程在哪些 CPU 内核上运行?的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  4. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  7. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  8. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  9. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  10. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

随机推荐