python中阻止的多处理

2024-09-21 03:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

多重处理不起作用。他们好像被封锁了。你知道吗

我的代码的输出是

filename2examples, page=1
filename2examples, page=2
...
filename2examples, page=18
filename2examples, page=19
filename2examples, page=20
MULTI, N_PROCESS=4
pool

与此相关的代码是

arg_lst = []
df_y = row_data
for idx, (page, df_y_page) in enumerate(list(df_y.groupby("page"))):
    print("filename2examples, page={}".format(page))
    hw = temml_data[page][0]
    page_text = temml_data[page][2]
    height = hw.iloc[0]["height"]
    width = hw.iloc[0]["width"]
    arg_lst.append([df_y_page, height, width, page_text])

if N_PROCESS > 1:
    print("MULTI, N_PROCESS={}".format(N_PROCESS))
    with multiprocessing.Pool(N_PROCESS) as pool:
        print("pool")
        page_examples_lst = pool.starmap(get_page_examples, arg_lst)
        for page_examples in page_examples_lst:
            yield page_examples
else:
   for arg in arg_lst:
       df_y_page, height, width, page_text = arg
       page_examples = get_page_examples(df_y_page, height, width, page_text)
       yield page_examples

get_page_examples

def get_page_examples(df_y_page, height, width, page_text):
    print("get_page_examples")
    arg_lst = []
    for i, (idx, row) in enumerate(df_y_page.iterrows()):
        cur_row = copy.deepcopy(row)
        cur_row["height"] = height
        cur_row["width"] = width
        tml_start, tml_finish = row["tml_start"], row["tml_finish"]
        arg_lst.append([tml_start, tml_finish, page_text, cur_row])

    page_examples = []
    for arg in arg_lst:
        tml_start, tml_finish, page_text, cur_row = arg
        example = find_x_text_and_process_row(tml_start, tml_finish, page_text, cur_row)
        page_examples.append(example)
    return page_examples

从程序的输出可以看出,它被阻塞了,并且没有进入get_page_examples函数。你知道吗

linux机器的cpu计数是16。 在linux机器上,当我设置N\u PROCESS=1时,代码将起作用;当我设置N\u PROCESS>;1时,代码将不起作用。你知道吗

当我使用windows机器时,无论N\u进程是1还是大于1,它都能工作。你知道吗

我用了要求.txt在我的windows机器上生成docker映像,然后在我的linux机器上运行,但也失败了。你知道吗

另外,无论linux机器上的python解释器是3.5还是3.6,它都无法工作。你知道吗

当我测试python多处理模块时,它将工作:

>>> import os
>>> os.cpu_count()
16
>>> import multiprocessing
>>> multiprocessing.cpu_count()
16
>>> import multiprocessing
>>> with multiprocessing.Pool(4) as pool:
...     pool.map(print, [1, 2, 3])
... 
1
2
3
[None, None, None]

谢谢你的回答。你知道吗


Tags: textdfgetpageargwidthprocessexamples

热门问题