Here is the code:
import multiprocessing as mp
import osdef process_wrapper(lineByte):
with open(“input.txt”) as f:
f.seek(lineByte) # move lineByte positions to put the pointer there
line = f.readline()
#init objects
pool = mp.Pool(mp.cpu_count())
jobs =if name == ‘main’:
#create jobs
with open("./test.txt",“rb”) as f:
nextLineByte = f.tell() # current position of the pointer
for line in f:
jobs.append( pool.apply_async(process_wrapper,(nextLineByte)) )
nextLineByte = f.tell()#wait for all jobs to finish
for job in jobs:
job.get()#clean up
pool.close()
After running, I got this error message as the following.
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
Any comments are greatly appreciated.