0

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.

hashcode55
  • 5,622
  • 4
  • 27
  • 40
Misha AM
  • 137
  • 3
  • 13
  • Why not just pass `LPV` as argument to the process? – hashcode55 Feb 23 '17 at 20:19
  • Tried that, but then nothing happens in parallel. It just launches the first worker, and waits for it to finish. – Misha AM Feb 23 '17 at 21:00
  • Can you edit the question with the code in which you passed the arguments ? And you cannot use `Vault` type objects with `Value`, just the types which are in `ctypes` – hashcode55 Feb 23 '17 at 21:10
  • Thanks. So I tired passing the Vault, as so: Process(target=target=workerDEV(), args=(LPV)). Got an error. Which is what you said not to do. Then I tried passing it like so: worker_1 = multiprocessing.Process(target=workerDEV(LPV)- it worked, but only one worker launched. Others did nothing. Is that what you mean by adding my code? – Misha AM Feb 23 '17 at 21:50

1 Answers1

1

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()
hashcode55
  • 5,622
  • 4
  • 27
  • 40