When attempting to append process id to list inside processing_task the list is changed but when attempting to return it from function it is unchanged for some reason I don't understand what the problem is...
Goal is :
1.Pass all_process list to function
2.Append process id to list
3.Get that appended list returned
4.Passed onto function that can terminate the process(in this case
keyboard.add_hotkey('ctrl+shift+q', kill_all(a_processes=xzy)))
from multiprocessing import Manager, Process, Pool
from multiprocessing.managers import NamespaceProxy, BaseManager
import inspect # part of multiprocessing stuff
import time
import keyboard
import os
class ObjProxy(NamespaceProxy):
"""Returns a proxy instance for any user defined data-type. The proxy instance will have the namespace and
functions of the data-type (except private/protected callables/attributes). Furthermore, the proxy will be
pickable and can its state can be shared among different processes. """
@classmethod
def populate_obj_attributes(cls, real_cls):
DISALLOWED = set(dir(cls))
ALLOWED = ['__sizeof__', '__eq__', '__ne__', '__le__', '__repr__', '__dict__', '__lt__',
'__gt__']
DISALLOWED.add('__class__')
new_dict = {}
for (attr, value) in inspect.getmembers(real_cls, callable):
if attr not in DISALLOWED or attr in ALLOWED:
new_dict[attr] = cls._proxy_wrap(attr)
return new_dict
@staticmethod
def _proxy_wrap(attr):
""" This method creates function that calls the proxified object's method."""
def f(self, *args, **kwargs):
return self._callmethod(attr, args, kwargs)
return f
attributes = ObjProxy.populate_obj_attributes(Process)
ProcessProxy = type("ProcessProxy", (ObjProxy,), attributes)
def processing_task(function, normal_processes, combat_processes, all_processes,
arguments=None, append="ALL"):
BaseManager.register('Process', Process, ProcessProxy, exposed=tuple(dir(ProcessProxy)))
manager = BaseManager()
manager.start()
if arguments is None:
start_function = manager.Process(target=function)
else:
start_function = manager.Process(target=function, args=arguments)
# APPENDING
if append == "ALL":
all_processes.append(start_function)
print(all_processes) # THIS IS THE VALUE THAT SHOULD BE RETURNED
elif append == "no":
normal_processes.append(start_function)
elif append == "co":
combat_processes.append(start_function)
start_function.start()
print(normal_processes, combat_processes, all_processes)
n_processes = normal_processes
c_processes = combat_processes
a_processes = all_processes
return n_processes, c_processes, a_processes
def counting():
for i in range(1, 10, 1):
time.sleep(1)
print(i)
def kill_all(a_processes):
time.sleep(1)
print("ATTEMPTING TO KILL : ", a_processes)
for process in a_processes:
# print(process)
process.terminate()
process.kill()
process.join()
exit()
os._exit(0)
if __name__ == '__main__':
listmanager = Manager()
no_processes = listmanager.list()
co_processes = listmanager.list()
all_processes = listmanager.list()
n, c, xzy = processing_task(counting, no_processes, co_processes, all_processes)
print("PRINTING ALL_PROC", all_processes)
print("xzy[all_processes] REMAINS UNCHANGED", xzy)# OUTPUTS VALUE Same as all_processes BEFORE processing_task SHOULD output value i mention inside processing_task
keyboard.add_hotkey('ctrl+shift+q', kill_all(a_processes=xzy))