log写入

This commit is contained in:
huihun 2024-03-03 16:39:19 +08:00
parent 835b23f7be
commit 1317c38bc6

View File

@ -3,7 +3,7 @@ import os
import subprocess import subprocess
import threading import threading
import time import time
from log_utils import setup_logger
# 设置最大并发线程数 # 设置最大并发线程数
max_threads = 20 max_threads = 20
@ -15,28 +15,26 @@ threads_completed = 0
condition = threading.Condition(thread_lock) condition = threading.Condition(thread_lock)
timer_event = threading.Event() timer_event = threading.Event()
def execute_command(cmd):
def execute_command(cmd, log):
""" """
在子线程中执行给定的命令 在子线程中执行给定的命令
""" """
global active_threads, threads_completed global active_threads, threads_completed
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
with condition: with condition:
# print("Command: %s" % cmd)
if stdout: if stdout:
print("stdout: %s" % stdout.decode('gbk')) log.info("stdout: %s" % stdout.decode('gbk'))
if stderr: if stderr:
print("stderr: %s" % stderr.decode('gbk')) log.warning("stderr: %s\n err_cmd: %cmd" % (stderr.decode('gbk'), cmd))
# 当前线程完成任务后释放一个线程位置 # 当前线程完成任务后释放一个线程位置
active_threads -= 1 active_threads -= 1
threads_completed += 1 threads_completed += 1
condition.notify_all() condition.notify_all()
print threads_completed print threads_completed
return (stdout, stderr) # 返回结果(可选)
def timer_thread(): def timer_thread():
print 'start timer thread' print 'start timer thread'
@ -57,7 +55,7 @@ if __name__ == '__main__':
# timer = threading.Thread(target=timer_thread) # timer = threading.Thread(target=timer_thread)
# timer.start() # timer.start()
# timer_event.clear() # timer_event.clear()
log = setup_logger('thread_out', 'ida_asm_create_out.log')
# 样本文件夹 # 样本文件夹
sample_dir = "D:/bishe/dataset/sample_20230130_458" sample_dir = "D:/bishe/dataset/sample_20230130_458"
# 创建并启动线程 # 创建并启动线程
@ -65,7 +63,7 @@ if __name__ == '__main__':
for file in os.listdir(sample_dir): 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 ' 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 = [] threads = []
for cmd in commands: for cmd in commands:
while active_threads >= max_threads: while active_threads >= max_threads:
@ -73,12 +71,11 @@ if __name__ == '__main__':
# 等待有线程完成任务 # 等待有线程完成任务
condition.wait() condition.wait()
thread = threading.Thread(target=execute_command, args=(cmd,)) thread = threading.Thread(target=execute_command, args=(cmd, log))
thread.start() thread.start()
active_threads += 1 active_threads += 1
threads.append(thread) threads.append(thread)
# 等待所有线程完成 # 等待所有线程完成
for thread in threads: for thread in threads:
thread.join() thread.join()