From 1317c38bc6f1d4e7d857622ace361cb300ce0439 Mon Sep 17 00:00:00 2001 From: huihun <781165206@qq.com> Date: Sun, 3 Mar 2024 16:39:19 +0800 Subject: [PATCH] =?UTF-8?q?log=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Genius3/raw-feature-extractor/thread.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Genius3/raw-feature-extractor/thread.py b/Genius3/raw-feature-extractor/thread.py index f55023f..6abf3e6 100644 --- a/Genius3/raw-feature-extractor/thread.py +++ b/Genius3/raw-feature-extractor/thread.py @@ -3,7 +3,7 @@ import os import subprocess import threading import time - +from log_utils import setup_logger # 设置最大并发线程数 max_threads = 20 @@ -15,28 +15,26 @@ threads_completed = 0 condition = threading.Condition(thread_lock) timer_event = threading.Event() -def execute_command(cmd): + +def execute_command(cmd, log): """ 在子线程中执行给定的命令。 """ global active_threads, threads_completed - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() with condition: - # print("Command: %s" % cmd) if stdout: - print("stdout: %s" % stdout.decode('gbk')) + log.info("stdout: %s" % stdout.decode('gbk')) if stderr: - print("stderr: %s" % stderr.decode('gbk')) - + log.warning("stderr: %s\n err_cmd: %cmd" % (stderr.decode('gbk'), cmd)) # 当前线程完成任务后释放一个线程位置 active_threads -= 1 threads_completed += 1 condition.notify_all() print threads_completed - return (stdout, stderr) # 返回结果(可选) + def timer_thread(): print 'start timer thread' @@ -57,7 +55,7 @@ if __name__ == '__main__': # timer = threading.Thread(target=timer_thread) # timer.start() # timer_event.clear() - + log = setup_logger('thread_out', 'ida_asm_create_out.log') # 样本文件夹 sample_dir = "D:/bishe/dataset/sample_20230130_458" # 创建并启动线程 @@ -65,7 +63,7 @@ if __name__ == '__main__': for file in os.listdir(sample_dir): com = r'D:\IDA_Pro_v6.8\idaq64.exe -c -A -S"D:\bishe\Gencoding_KE\Genius3\raw-feature-extractor\preprocessing_ida.py 0" -oD:\bishe\dataset\out ' - commands.append(com+"D:/bishe/dataset/sample_20230130_458/"+file) + commands.append(com + "D:/bishe/dataset/sample_20230130_458/" + file) threads = [] for cmd in commands: while active_threads >= max_threads: @@ -73,12 +71,11 @@ if __name__ == '__main__': # 等待有线程完成任务 condition.wait() - thread = threading.Thread(target=execute_command, args=(cmd,)) + thread = threading.Thread(target=execute_command, args=(cmd, log)) thread.start() active_threads += 1 threads.append(thread) - # 等待所有线程完成 for thread in threads: thread.join()