pythonparallel-processingmultiprocessinglastpass

How to pass a lastpass Vault object into multiprocessing process worker


I am trying to run multiple processes all of which use information from my Lastpass credentials. The idea is to grab the Vault once, and then use a bunch of Workers retrieving passwords to do their jobs.

I get the vault -

if __name__ == '__main__':
    LPV=lp.get_vault()

I Define workers:

def workerDEV():
   environment='DEV'
   print "I am in ", environment
   create_objects.main(client=client, LastPassVault=LPV)

def workerPRD():
   environment='PRD'
   print "I am in ", environment
   create_objects.main(client=client, LastPassVault=LPV)

Launch the workers:

worker_1 = multiprocessing.Process(target=workerDEV)
worker_1.start()

worker_2 = multiprocessing.Process(target=workerPRD)
worker_2.start()

I get the error:

NameError: global name 'LPV' is not defined

This makes sense, since as per my research, each worker re starts the session, thus loosing LPV, which is protected by if __name__ == '__main__':.

I looked into multiprocessing.Value(typecode_or_type, *args, lock=True), but can't figure out how to use it. It seems like it is meant for objects of type String and Int, but not a Vault.

Thank you, and recommendations from here are highly appreciated.


Solution

  • Replace this as your worker function to remove redundancy and pass LPV as a parameter to your worker function.

    from multiprocessing import Process
    
    def workerFunc(num, LPV): 
        print "I am in ", num 
        create_objects.main(client=client, LastPassVault=LPV)
    
    for i in range(2): 
        Process(target=workerFunc, args=(i, LPV)).start()