所有Python实例是否都在同一个线程上运行?

huangapple go评论65阅读模式
英文:

Do all instances of python run on the same thread?

问题

我了解关于Python中线程、多线程和多进程的讨论,但我的问题是,是否调用的所有Python实例都并行运行?
我不是建议使用单个Python脚本来生成多个线程,而是在不同的终端中同时启动相同的脚本。它们会真正并行吗?
只是提到了这个可能是相同的问题,但我的知识不足以确定。
还是Python解释器被锁定,无论如何启动的所有进程都需要使用相同的解释器来运行?

英文:

I'm aware of the discussions around threading, multithreading, and multiprocessing in python, but my question is whether all instances of python that are called run in parallel?
I'm not suggesting using a single python script to spawn many threads, but literally starting the same script a few times in different terminals. Will they be in true parallel?
Just citing that this might be the same question with the answer, but my knowledge isn't sufficient to be sure.
Or is the python interpreter locked so all processes no matter how they're started still need to use the same interpreter to operate?

答案1

得分: 1

Maybe, but it doesn't really have anything to do with Python itself.

Depending on your system and your hardware, the different processes may run in parallel, but it depends. Take for example a single core machine with no threading (rare these days, but very possible): because it can only possibly run one process at a time, then there's no possible way it could run parallel. If you have an operating system and a shell that's smart, then it might run them in parallel, but there's nothing that says it must.

If I had to guess, I'd say you can safely your (remotely modern) machine does run the processes at the same time. However, if you're running it on some kind of embedded system this might not be the case.

英文:

Maybe, but it doesn't really have anything to do with Python itself.

Depending on your system and your hardware, the different processes may run in parallel, but it depends. Take for example a single core machine with no threading (rare these days, but very possible): because it can only possibly run one process at a time, then there's no possible way it could run parallel. If you have an operating system and a shell that's smart, then it might run them in parallel, but there's nothing that says it must.

If I had to guess, I'd say you can safely your (remotely modern) machine does run the processes at the same time. However, if you're running it on some kind of embedded system this might not be the case.

huangapple
  • 本文由 发表于 2023年7月18日 09:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定