运行多个Python脚本 [英] Running multiple Python scripts

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

问题描述

我想创建一个简单的Python程序,该程序将同时执行2个独立的脚本.目前,这两个脚本只显示一个数字序列,但我的目的是将来使用此程序并发运行一些Twitter流程序.

I would like to create a simple Python program that will concurrently execute 2 independent scripts. For now, the two scripts just print a sequence of numbers but my intention is to use this program to concurrently run a few Twitter streaming programs in the future.

我怀疑我需要使用subprocess.Popen,但是我不太了解应该在其中添加哪些参数.在StackOverflow上也有类似的问题,但是那里提供的代码(粘贴在下面)无法显示任何内容.感谢您的帮助.

I suspect I need to use subprocess.Popen but I cannot quite get my head around what arguments I should put in there. There was a similar question on StackOverflow but the code provided there (pasted below) doesn't print anything. I will appreciate your help.

我的文件是:

thread1.py
thread2.py

thread1.py
thread2.py

import subprocess

subprocess.Popen(['screen', './thread1.py']))
subprocess.Popen(['screen', './thread2.py'])

推荐答案

您还可以打开几个命令提示符窗口来一次运行多个Python程序-只需在其中每个程序中运行一个即可.

You can also just open several Command Prompt windows to run several Python programs at once - just run one in each of them:

在每个命令提示符窗口中,转到正确的目录(例如C:/Python27),然后在一个命令提示符窗口中键入"python YourCodeNo 1 .py",即"python YourCodeNo 2 .py". .

In each Command Prompt window, go to the correct directory (such as C:/Python27) and then type 'python YourCodeNo1.py' in one Command Prompt window, 'python YourCodeNo2.py' in the next one ect. .

我目前正在以这种方式一次运行3个代码,而不会降低任何代码的速度.

I'm currently running 3 codes at one time in this way, without slowing any of them down.

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

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