0

I'm having a problem with the Python multiprocessing package.

I'm using this method to run the code and use the multiprocessing module:

    import multiprocessing
    from multiprocessing.dummy import Pool as ThreadPool

    def change_file(simulation):
        for line in fileinput.FileInput(directory + '/file.txt', inplace=1):        
            for item in range(1,num_variables+1):
                line = line.replace('some text','other text')
            print (line.rstrip('\n'))

   pool = ThreadPool(2) 
   results = pool.map(change_file, Lists_of_works)
   pool.close() 
   pool.join()

This code it’s a component of a parametric program, and the program creates folders to identify simulations. Then inside each folder replace some text (different depending of each case) of a .txt file. The ‘directory’ name and the replaced text depend on the simulations. To perform the simulations I use multithreading. And this is the only component of the code that needs to use 1 core, because when I use 4, I get the following error:

ValueError: I/O operation on closed file.

I think the reason is because the main process open the file and the child close it…so the new main process use the same file. This reason it is explained in this post

SOLVED: The solution is to store the text file on a global variable (string) and then replace the text as the post explains.

Thanks!

Community
  • 1
  • 1
GermanR
  • 3
  • 2
  • you use multiprocessing to edit the same file? why you need that :) – linpingta Oct 19 '16 at 11:09
  • Please add clarification about what the following variables: num_variables, lists_of_works. Also, apart from just pasting code that does not work, you should clearly state what exactly you are trying to do in order for other people to help you. – lesingerouge Oct 19 '16 at 11:49
  • SOLVED as it is explained in the post, with globals() – GermanR Oct 22 '16 at 05:07

0 Answers0