[英]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.py
和second.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.