Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

Python Multiprocess good practices

i am working on a software which is treating a lot of data, so we've used multiproccess to accelerate the treatment. here is my actual code :

for file_to_process in file_partition:
    if (len(file_to_process) > 0):
       logger = self.__get_logger__(task.lower(), len(proc_list) + 1)
       proc = Process(target=self.__launch_thread__, args=(task, manager_class, file_to_process, list_action, str_target_path, str_target_buffer_path, logger))
       proc.start()
       proc_list.append(proc)

for proc in proc_list:
    proc.join()

The thing is that we've noticed that our procs are not realasing memory after they are finished so i was thinking about doing :

for file_to_process in file_partition:
    if (len(file_to_process) > 0):
       logger = self.__get_logger__(task.lower(), len(proc_list) + 1)
       proc = Process(target=self.__launch_thread__, args=(task, manager_class, file_to_process, list_action, str_target_path, str_target_buffer_path, logger))
       proc.start()
       proc_list.append(proc)

for proc in proc_list:
    if(!(proc.is_alive())):
        proc.close()

I've also seen i could use pool but i am not sure about the interest of using it. I am also not really sure about how the join() method works, i've read the docs but it seems that procs don't really wait for each other.

So if someone can enlight me on how to use this multiprocess lib properly that would be great

question from:https://stackoverflow.com/questions/65952048/python-multiprocess-good-practices

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looking at the code for Process, it will generate the following error message if the right condition is not met:

Cannot close a process while it is still running. You should first call join() or terminate().

So simply mofify your existing code to be:

for proc in proc_list:
    proc.join()
    proc.close()

Whether that will help much is to be decided.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...