Python Forum
Multiprocessing Can't pickle local object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocessing Can't pickle local object
#1
I finally decided to experiment on the multiprocessing module, however I run straight into a ditch where am currently hitting my head against it's walls. I desperately need some help to get out of this, here is an example of my code structure:-
import time 
import multiprocessing

    

def vcf():
        
        d='578'      
        d1='556'       
        e='678'
        e1='64498'
        
        def abc(d,e):
                
                print(d)
                print(e)
          
        def cab(d1,e1):
        
                print(d1)
                print(e)

            
        p1 = multiprocessing.Process(target=abc,args=(d,e,))
        p2 = multiprocessing.Process(target=cab,args=(d1,e1,))
        if __name__ == "__main__":
                
                p1.start()
                p2.start()
                
                p1.join()  
                p2.join()   

vcf()

finish = time.perf_counter()
print("Finished running after seconds : ",finish)


How do I make it to run while maintaining this structure, that is without moving def abc and def cab to the Top Level Function
Reply
#2
Incase anyone else faces the same error with the same code structure, adding global keyword word to the child function before you define it eliminates the error. I have no idea why or how it works, but it works...

import time 
import multiprocessing
 
     
 
def vcf():
        global abc
        global cab

        d='578'      
        d1='556'       
        e='678'
        e1='64498'
         
        def abc(d,e):
                 
                print(d)
                print(e)
           
        def cab(d1,e1):
         
                print(d1)
                print(e)
 
             
        p1 = multiprocessing.Process(target=abc,args=(d,e,))
        p2 = multiprocessing.Process(target=cab,args=(d1,e1,))
        if __name__ == "__main__":
                 
                p1.start()
                p2.start()
                 
                p1.join()  
                p2.join()   
 
vcf()
 
finish = time.perf_counter()
print("Finished running after seconds : ",finish
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 268 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  pickle problem DPaul 13 4,538 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,606 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  multiprocessing and sharing object cyrduf 0 2,010 Feb-02-2021, 08:16 PM
Last Post: cyrduf
  Save/Loading using pickle Scordomaniac 4 2,977 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  Multiprocessing, class, run and a Queue Object SeanInColo 0 1,491 Jul-12-2020, 05:36 PM
Last Post: SeanInColo
  computing entropy using pickle files baran01 2 2,374 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Tkinter don't get ver from file via pickle storzo 2 2,519 Jul-31-2019, 03:50 PM
Last Post: storzo
  pickle docs say bytes in one place, strings in another Skaperen 2 2,106 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 10,860 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020