我從蘋果應用商店下載了一個簡單的 .exe。它提供有關加密貨幣價格及其百分比變化的實時更新。我正在提取位元幣的百分比變化。
我正在使用子行程來提取輸出。我將輸出存盤到四個單獨的文本檔案中,然后從檔案中讀取文本,提取我需要的資料并將其保存到 pandas 資料框中。
此外,每次運行 .exe 都有 60 秒的超時時間,并且在 70 秒后讀取檔案。我通過洗掉檔案的內容來截斷檔案,當下一個檔案有輸出時,我也會讀取該檔案,然后截斷并重復。
我想知道如何在將輸出保存到文本檔案和讀取內容然后截斷它之間拆分作業。例如,我正在運行一個簡單的執行緒,它應該運行 .exe 并使用 .exe 提取輸出write_truncate_output。但是,我只有append_output_executable跑步。
這是我的腳本:
from subprocess import STDOUT, check_call as x
import os
from multiprocessing import Pool
import time
import re
from collections import defaultdict
import pandas as pd
from multiprocessing import Process
cmd = [r'/Applications/CryptoManiac.app/Contents/MacOS/CryptoManiac']
text_file = ['bitcoin1.txt','bitcoin2.txt','bitcoin3.txt','bitcoin4.txt']
def append_output_executable(cmd):
while True:
i = '1234'
for num in i:
try: #append the .exe output to multiple files
with open(os.devnull, 'rb') as DEVNULL, open('bitcoin{}.txt'.format(num), 'ab') as f:
x(cmd, stdout=f, stderr=STDOUT, timeout=60)
except:
pass
def write_truncate_output(text):
while True:
time.sleep(70)
with open(text, 'r ') as f:
data = f.read()
f.truncate(0)
#read and truncate after reading the data
#filter and format
percentage=re.findall(r'\bpercent_change_24h:\s.*', data)
value= [x.split(':')[1] for x in percentage]
key = [x.split(':')[0] for x in percentage]
#store in dictionary
percent_dict = defaultdict(list)
for ke, val in zip(key, value):
percent_dict[ke].append(val)
percent_dict['file'].append(text)
percent_frame = pd.DataFrame(percent_dict)
print(percent_frame)
if __name__ == '__main__':
for text in text_file:
execute_process = Process(target = append_output_executable, args=(cmd,))
output_process = Process(target = write_truncate_output, args=(text,))
execute_process.start()
execute_process.join()
output_process.start()
output_process.join()
這仍然只是運行第一個函式。
uj5u.com熱心網友回復:
我不知道這個答案是否能解決您所有的問題,因為您在程式中幾乎沒有錯誤 - 當您修復一個錯誤時,它仍然無法正常作業,因為還有其他錯誤。
第一的:
target在Thread并且Process需要不帶()引數的函式名稱 - (這被稱為callback) - 稍后(當你使用時.start())它將用于()在 newThread或Process
Thread(target=append_output_executable, args=(cmd,))
Process(target=append_output_executable, args=(cmd,))
第二:
Thread并且Process只運行一次這個函式,所以它需要while-loop 一直運行。它不能使用return,因為它結束了功能。
第三:
.join()阻塞代碼,因為它等待Thread或結束,Process應該在啟動所有執行緒/行程后使用它 - 通常當你想停止所有執行緒/行程時在程式結束時使用它
和小建議:
您可以使用全域running = True和內部函式while running- 稍后您可以設定running = False停止函式中的回圈(并完成函式)
代碼可能看起來像這樣
# ... other imports ...
from threading import Thread
def append_output_executable(cmd):
while running:
# ... code ... (without `return`)
def write_truncate_output(text):
while running:
# ... code ... (without `return`)
# --- main ---
# global variables
running = True
if __name__ == '__main__':
# --- create and start ---
t0 = Thread(target=append_output_executable, args=(cmd,))
t0.start()
other_threads = []
for text in text_file:
t = Thread(target=write_truncate_output, args=(text,))
t.start()
other_threads.append(t)
# ... other code ...
# --- at the end of program ---
running = False
# --- wait for end of functions ---
t0.join()
for t in other_threads:
t.join()
完全一樣的是Process
(我保留相同的變數名稱以表明一切都是一樣的)
# ... other imports ...
from multiprocessing import Process
def append_output_executable(cmd):
while running:
# ... code ... (without `return`)
def write_truncate_output(text):
while running:
# ... code ... (without `return`)
# --- main ---
# global variables
running = True
if __name__ == '__main__':
# --- create and start ---
t0 = Process(target=append_output_executable, args=(cmd,))
t0.start()
other_threads = []
for text in text_file:
t = Process(target=write_truncate_output, args=(text,))
t.start()
other_threads.append(t)
# ... other code ...
# --- at the end of program ---
running = False
# --- wait for end of functions ---
t0.join()
for t in other_threads:
t.join()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/475205.html
