簡體   English   中英

將python腳本導入其他腳本並同時運行

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

我有兩個python文件first.pysecond.py ,都希望它們同時工作,所以我嘗試使用此模塊導入文件

import first.py 
import second.py

但其中只有一個正在工作。

如何同時運行兩者?

- -編輯 - -

我嘗試了多線程,

但仍然沒有運氣:(,仍然只有兩個能正常工作

- -編輯 - -

解決了縮進錯誤

您需要像這樣使用線程和子流程:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM