多处理&多线程 - 理论澄清
我有两个与Python并发有关的问题,他们希望有人澄清。
任务说明:
让我们说我设置了两个PY脚本。每个脚本都使用多线程(最大工人为2)运行两个IO结合任务(API调用)。
问题:
如果我不使用虚拟环境,并通过全局Python解释器(系统范围内的Python安装中的一个)运行两个脚本。这是否使我描述了单个过程和多线程?既然我们使用一个解释器(单个过程),并且有两个脚本运行了4个线程?
如果我使用Pycharm创建两个独立的项目,每个项目都有自己的Python解释器。这样的设置是否将任务转化为多处理和多线程?既然我们有两个Python解释器运行,并且每个都运行了两个线程?
I have two Python concurrent-related questions that want someone's clarification.
Task Description:
Let us say I set up two py scripts. Each script is running two IO-bound tasks (API Calls) with multithreading (max workers as 2).
Questions:
If I don't use a virtual environment, and run both scripts through the global Python interpreter (the one in system-wide Python installation). Does this make the task I described the single process and multithreaded? Since we are using one interpreter (single process) and have two scripts running a total of 4 threads?
If I use the Pycharm to create two separate projects where each project has its own Python interpreter. Does such a setting turn the task into multiprocessing and multithreaded? Since we have two Python interpreters running and each running two threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个运行的解释器过程都有自己的GIL,与其他碰巧正在运行的解释者中的任何其他GIL分开。与正在运行的脚本相关的项目和虚拟环境无关紧要。虚拟环境是要隔离Python和库的不同版本,因此一个项目的库不会干扰另一个项目的库。
如果您像
python脚本。
如果这两个过程完全彼此独立并且从不说话,我认为将其称为“多进程任务”是没有意义的。您有多个流程正在运行,但是在Python程序的上下文中“多处理”通常意味着一个连贯的程序,该程序使用多个过程用于常见任务。
Each running interpreter process has its own GIL that's separate from any other GILs in other interpreters that happen to be running. The project and virtual environment associated with the script being run are irrelevant. Virtual environments are to isolate different versions of Python and libraries so libraries from one project don't interfere with libraries in another.
If you run two scripts separately like
python script.py
, this will start two independent interpreters that will be unaffected by the other.I don't think it's really meaningful to call it a "multiprocess task" if the two processes are completely independent of each other and never talk. You have multiple processes running, but "multiprocessing" within the context of a Python program typically means one coherant program that makes use of multiple processes for a common task.