Write data to disk in Python as a background process

前端 未结 3 942
礼貌的吻别
礼貌的吻别 2021-02-04 17:59

I have a program in Python that basically does the following:

for j in xrange(200):
    # 1) Compute a bunch of data
    # 2) Write data to disk
<
3条回答
  •  轮回少年
    2021-02-04 18:26

    Simple way would be to use just threading and the queue. On the other hand, if the computing part does not depend on global state, and you have machine with multiple CPU cores, more efficient way would be to use process pool

    from multiprocessing import Pool
    
    def compute_data(x):
        return some_calculation_with(x)
    
    if __name__ == '__main__':
        pool = Pool(processes=4) # let's say you have quad-core, so start 4 workers
    
        with open("output_file","w") as outfile:
            for calculation_result in pool.imap(compute_data, range(200)):
            # pool.imap returns results as they come from process pool    
                outfile.write(calculation_result)  
    

提交回复
热议问题