问题
I am working on task where I am creating multiple process to run code in parallel to speed up process. below is my code.
def update_value(value):
print('module name:\n', __name__)
print('parent process:\n', os.getppid())
print('process id:\n', os.getpid())
value_read = server_connect_read(channel, value)
if value_read.server_connect() is False:
return False
print("updating values")
update = server_read.update_value(old_values.xlsx)
if value_read.server_disconnet() is False:
return False
Pool(3, initializer=print('starting', current_process().name )).map(update_value, (ValueList,))
In Above code, ValuList is excel file containing values that needed to update. Now when I run above code I am getting below as output.
module name:
__main__
parent process:
8048 <-----
process id:
15068 <-----
module name:
__main__
parent process:
8048 <-----
process id:
15068 <----
In the process, first code will reads value from local file, establish connection, reads value from server, updates to local file.
Above code runs and I can see process is getting created. But all process parent id and process id remains same. As per my understanding, each process will have their own process id's.
I need help in figuring out if any mistakes in the code.
回答1:
After doing some more searches over the topic I found that the process does a fork() followed by an execve() of a completely new Python process. That solved problem, because module state starts from scratch. In code as below.
if __name__ == '__main__':
with get_context("spawn").Pool(2) as pool:
pool.map(update_value, ValueList)
Hope this helps
来源:https://stackoverflow.com/questions/61093146/multiple-process-is-not-getting-created-python