Python3.xWindows多处理
我读过很多关于Windows和linux多处理之间差异的主题,但仍然找不到答案。 我有这样的脚本:Python3.xWindows多处理,python,python-3.x,Python,Python 3.x,我读过很多关于Windows和linux多处理之间差异的主题,但仍然找不到答案。 我有这样的脚本: def create_file(): #here user define output file name def READ_MANY_LOG(): #here user define path to log files and we read all these logs to one list() def select_unic_value(READ_MANY_LOG):
def create_file():
#here user define output file name
def READ_MANY_LOG():
#here user define path to log files and we read all these logs to one list()
def select_unic_value(READ_MANY_LOG):
#here specified unic value selected
def SEARCH_ENGINE(search_criteria, READ_MANY_LOG):
#here perform search from all logs with search_criteria and stored to multiprocess dict (return_dict)
def sort_all_logs_per_unic_value(create_file, select_unic_value):
mpc = 0
mpa = []
for n in select_unic_value:
search_criteria = criteria(n)
mps = Process(target=SEARCH_ENGINE, args=(search_criteria, READ_MANY_LOG))
mpa.append(mps)
mpc = mpc + 1
if mpc>=multiprocess.cpu_count():
for p in mpa:
p.start()
for p in mpa:
p.join()
For t_g in return_dict.values():
for t_x in t_g:
print(t_x,file=out, flush=True, end='')
print('PRINT done')
mpc = 0
mpa = []
return_dict = multiprocess.Manager().dict()
create_file()
READ_MANY_LOG()
select_unic_value(READ_MANY_LOG)
if __name__ == "__main__":
MULT.freeze_support()
SORT_BY_MAC_DIR(output_name)
它在linux上工作得非常好,因为多进程从create\u file()继承值
,读取多个日志()
,
选择单一值(读取多个日志)
并仅为搜索引擎启动多进程。
但是在windows上,由于使用了spawn,它会在不同的过程中启动整个脚本。windows是否有办法仅为特定部分启动多进程:搜索引擎
?
非常感谢。在linux和windows中,多进程产生了一个新进程。这在脚本需要专门访问cpu或内核时非常有用,否则称为cpu绑定。顺便说一句,您的应用程序看起来像是磁盘绑定的——因此您最好使用线程
在脚本场景中,您需要父进程在所有进程之间整理或共享值的方法。这包括调用进程和所有启动的子进程
可以使用进程池进行排序。例如,为您的流程创建一个日志数组。然后在最后加入结果
from multiprocessing import Pool
def SEARCH_ENGINE((sc, logs)):
# Do something with sc and logs
return myvalue
if __name__ == '__main__':
p = Pool(5)
# results will be a list of return values for each tuple listed
results = p.map(, [("First Search", "file1"), ("Second Search, ...)])
或者,您可以使用multiprocessing.Value
或multiprocessing.Array
使用共享内存,但是由于需要使用ctypes
,这会变得更复杂一些