Invoking a method in another process in Python

时间:2019-01-18 18:41:11

标签: python wxpython python-multiprocessing

I am trying to set up a non-blocking GUI with wxPython Phoenix using the multiprocessing library. Is there any functionality in Python or multiprocessing that allows you to invoke a method in another class in a different process similar to that in C#?

Without implementing multiprocessing, when the user clicks a sign in button with their credentials, it should contact the server and return a dictionary of their necessary information and then update the GUI accordingly. The issue arises when the server takes too long to respond, locking the GUI. I've separated the GUI and the server communication into their own processes and created a shared dictionary for them to access along with an event to alert the server process to begin authentication. I am back to square one as the event blocks the GUI until the dictionary is completed.

Within parallelization.py:

shared_event = multiprocessing.Event()
user_info_dict = manager.dict()
run_bool = multiprocessing.Value(ctypes.c_bool, True)

process_pool = multiprocessing.Pool(
        2,
        initializer=self.thread_init,
        initargs=(run_bool, shared_event, user_info_dict)
)

gui_thread_results = process_pool.map_async(self.thread_gui, ['A'])
server_comm_thread_results = 
   process_pool.map_async(self.thread_server_comm, ['B']) 

def thread_gui(self, args):

    # pass in shared threading objects and initialize the GUI
    interface.user_int().run(thread_bool=thread_bool,
                             thread_event=thread_event,
                             thread_user_info_dict=thread_user_info_dict)

def thread_init(self, passed_bool, passed_event, passed_dict):

    global thread_bool
    thread_bool = passed_bool
    global thread_event
    thread_event = passed_event
    global thread_user_info_dict
    thread_user_info_dict = passed_dict

def thread_server_comm(self, args):

    while thread_bool.value:

        thread_event.clear()
        thread_event.wait()

        dict = authenticator.get_user_info(user, pass)

        # somehow alert the GUI process to update the GUI with the 
        # completed dictionary without making the GUI process wait on the 
        # event

0 个答案:

没有答案
相关问题