简体   繁体   English

将python脚本导入其他脚本并同时运行

[英]import python script to other and run both in the same time

I have two python files first.py and second.py , want both work in the same time so i tried to use this module import file 我有两个python文件first.pysecond.py ,都希望它们同时工作,所以我尝试使用此模块导入文件

import first.py 
import second.py

but one only of them is working. 但其中只有一个正在工作。

how to run both in the same time? 如何同时运行两者?

---EDIT--- - -编辑 - -

i tried multi-threading and 我尝试了多线程,

but still no luck :(, still one of two only is working 但仍然没有运气:(,仍然只有两个能正常工作

---EDIT--- - -编辑 - -

solved it was Indentation Error 解决了缩进错误

You need to use threading and subprocess like this: 您需要像这样使用线程和子流程:

import threading
import subprocess
import sys

def run_process(path):
    cmd = 'python %s' % path
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    for line in iter(process.stdout.readline, ''):
        sys.stdout.write(line)

t1 = threading.Thread(target=run_process, args=('path_to_first.py',))
t2 = threading.Thread(target=run_process, args=('path_to_second.py',))

#start
t1.start()
t2.start()

# Waiting
t1.join()
t2.join()

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

相关问题 同时运行 python 脚本和 flask 应用程序 - run python script and flask app at the same time 似乎无法在 Python 中的同一脚本中使用导入时间和导入日期时间 - Cannot seem to use import time and import datetime in same script in Python 如何同时运行 2 个脚本 python? - How run 2 script python in same time? Python 同时运行 2 个类,然后同时运行 2 个其他类 - Python Run 2 classes at the same time then 2 other classes at the same time 如何从相同的python脚本运行.py和.exe文件 - how to run both a .py and a .exe file from the same python script 在Google App Engine上同时运行php和python - Run both php and python at the same time on google app engine 如何使用不同的模块导入运行相同的 python 脚本? - How to run the same python script with different module import? 让 Python 程序运行另一个 Python 程序并让它们同时运行? - Make Python program run another Python program and have them both run at the same time? Python 脚本在 cron 作业中运行时引发错误,但不会在其他时间引发错误 - Python script elicits error when run in cron job but at no other time 如何同时在多个内核上运行多个输入的python脚本? - How to run python script for multiple input on multiple cores at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM