I get error when running the code
TypeError: Pickling an AuthenticationString object is disallowed for security reasons
I am very new to this... You can add your own completely new solution/code to this similar to my code as long as you can meet the goals(for example no need to use arrays to store process ids for termination etc...), goals are: 1.Running multiple processes in parallel and then when certain condition is met in STATECHECK 2.Kill processes that are appended to a sepcific Array (in this case main_processes,normal_processes) 3.Then terminate them from another process(i attempt to do so from reset() inside statecheck()) 4.Restart the same process(randomx) from statecheck
import multiprocessing
import time
from multiprocessing import Process
data_dict = {'y': 4,
'h': 5,
'j': 6,
'o': 7,
'p': 3,
'b': 11}
def randomx(key):
for i in range(0, 2, 1):
print(key)
time.sleep(data_dict[key])
def move():
print("Starting move2obj")
for i in range(0, 5, 1):
print(i)
def statecheck(np, cp):
print("statecheck started")
reset(np, cp)
time.sleep(10)
randomx()
time.sleep(20)
def run():
for key in data_dict:
key = Process(target=randomx, args=key)
print(main_processes, key)
main_processes.append(key)
all_processes.append(key)
key.start()
process24 = multiprocessing.Process(target=move)
process24.start()
process11 = Process(target=statecheck, args=[normal_processes, main_processes])
process11.start()
normal_processes.append(str(process24))
all_processes.append(str(process24))
all_processes.append(str(process11))
def reset(normal_processes, main_processes, *args):
time.sleep(1)
print("killing normal :", normal_processes)
for process in normal_processes:
process.terminate()
process.join()
time.sleep(1)
print("killing combat :", main_processes)
for process in main_processes:
process.terminate()
process.join()
def processes():
print(main_processes, normal_processes, all_processes)
return normal_processes, main_processes, all_processes
if __name__ == "__main__":
manager = multiprocessing.Manager()
normal_processes = manager.list()
main_processes = manager.list()
all_processes = manager.list()
run()
Here is a simpler way, relevant to your code, to reproduce the error you are getting:
Output
The reason why this happens has been explained in this answer. In short, you are trying to pickle something which is explicitly meant not to be pickled. That does not mean it cannot be worked around, however. You just need to use multiprocess, and make the the
authkeyof the process picklable:Output
So, for your code, instead of using the
Processclass, use the aboveMyProcessclass to create picklable processes.With that being said, unless you know what you are doing and are aware of the possible consequences, it's always better not to try and work around such "issues". They are there, by design, for a reason. So, instead of creating processes and adding them to a managed list (and then also passing that list to other processes), you should only join/terminate processes from the process that created them.
Edit
What do you mean by "demonstrate" data-transfer? Also, what you are doing will obviously raise an error since
a()does not know whatlis. You need to pass the managed list as an argument toafor that to work.Now about terminating processes from other processes, it's possible, but I can't really think of a use-case for such a thing which couldn't be done using other efficient methods. In short, if you are thinking along these lines you probably need to re-evaluate your whole approach.
If you still want to however, you can do this by creating the processes in another process using managers, and passing those instead of the actual processes to other processes. You won't need
multiprocessfor this, and neither will you need to alter theauthkeyof the processes themselves. Example:Output
Just use
manager.Processinstead of theProcessclass to create processes. Check this answer if you want more details as to how the manager and proxy above work.