简体   繁体   English

如何在 MacBook 上同时运行两个 python 程序?

[英]How to run two python programs on MacBook simultaneously?

I was wondering if I need to run two python programs simultaneously, can I open two terminal windows and run each of them at the same time?我想知道是否需要同时运行两个 python 程序,我可以打开两个终端窗口并同时运行它们吗? Will this slow down the computing?这会减慢计算速度吗? If so, is there any way to quickly run several programs at the same time?如果是这样,有什么办法可以同时快速运行多个程序? Like parallel computing.比如并行计算。 Cheers.干杯。

Can I open two terminal windows and run each of them at the same time?我可以打开两个终端窗口并同时运行它们吗?

You can do that but they won't run parallelly.您可以这样做,但它们不会并行运行。

Will this slow down the computing?这会减慢计算速度吗?

If you can run them in a truly parallel way, individual programs should not feel any slower than if they were run sequentially.如果您能够以真正并行的方式运行它们,那么单个程序应该不会比顺序运行更慢。

However, if you have multiple python scripts that you wish to run simultaneously in the same terminal, you can type the following lines on your bash terminal:但是,如果您希望在同一个终端中同时运行多个 python 脚本,您可以在 bash 终端上键入以下几行:

python3 script1.py &
python3 script2.py &

Or if you want to run them from another python script, you can do this:或者如果你想从另一个 python 脚本运行它们,你可以这样做:

# script3.py
import subprocess

subprocess.run("python3 script1.py & python3 script2.py", shell=True)

Or if you want to run multiple processes parallelly, you can take a look at python's built in multiprocessing module.或者,如果您想并行运行多个进程,您可以查看 python 的内置多处理模块。

Option1: Check using GNU Screen with vertical split选项 1:检查使用垂直分割的GNU Screen

Option2: Download iTerm2 for macOSX from this link .选项 2:从此链接下载适用于 macOSX 的 iTerm2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM