簡體   English   中英

從批處理文件同時運行兩個 python 腳本,使用 anaconda,不工作

[英]Running two python scripts simultaneously from batch file, using anaconda, not working

因此,根據thisthis ,要同時運行兩個 python 腳本,我應該從批處理文件中運行它們,並用&分隔。

但是,這似乎不適用於一個簡單的示例。

我目前正在嘗試這個:

測試1.py

import time
for i in range(5):
    time.sleep(1)
    print("Printing, test1:"+str(i))

測試2.py

import time
for i in range(5):
    time.sleep(2)
    print("Printing, test2:"+str(i))

批處理文件

call "C:\Users\Username\Anaconda3\Scripts\activate.bat"
"C:\Users\Username\Anaconda3\python.exe" "C:\Users\Python\Documents\Python\Test\test1.py" &
"C:\Users\Username\Anaconda3\python.exe" "C:\Users\Python\Documents\Python\Test\test2.py" &

我希望看到結果混合在一起,但是,結果如下: 結果3

很明顯,腳本 2 在腳本 1 之后運行。

有任何想法嗎?

謝謝!

使用 python 的多處理或線程模塊

import time
import threading
def test1():
    for i in range(5):
        time.sleep(1)
         print("Printing, test1:"+str(i))
def test2():
    for i in range(5):
        time.sleep(2)
        print("Printing, test2:"+str(i))
x = threading.Thread(target=test1)
t = threading.Thread(target=test2)
x.start()
t.start()

感謝@jasonharper 指出我發現的兩個解決方案是針對 Unix,而不是Windows(雖然我搜索過 Windows),我能夠找到另一個帖子,ZAEA23489CE3AA9B6406EBB28E0。

通過對 conda 稍加調整,我能夠讓機器人腳本同時運行,如下所示:

批處理文件

call "C:\Users\Username\Anaconda3\Scripts\activate.bat"
start python "C:\Users\Username\Documents\Python\Test\test1.py" &
start python "C:\Users\Username\Documents\Python\Test\test2.py" &

結果非常酷。兩個 python windows 同時運行: 結果2

感謝大家!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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