Multiprocessing with threading? Multiprocessing with threading? python-3.x python-3.x

Multiprocessing with threading?


You can generate a number of Processes, and then spawn Threads from inside them. Each Process can handle almost anything the standard interpreter thread can handle, so there's nothing stopping you from creating new Threads or even new Processes within each Process. As a minimal example:

def foo():    print("Thread Executing!")def bar():    threads = []    for _ in range(3): # each Process creates a number of new Threads        thread = threading.Thread(target=foo)         threads.append(thread)        thread.start()    for thread in threads:        thread.join()if __name__ == "__main__":     processes = []    for _ in range(3):        p = multiprocessing.Process(target=bar) # create a new Process        p.start()        processes.append(p)    for process in processes:        process.join()

Communication between threads can be handled within each Process, and communication between the Processes can be handled at the root interpreter level using Queues or Manager objects.


You can define a function that takes a process and make it run 3 threads and then spawn your processes to target this function, for example:

def threader(process):    for _ in range(3):        threading.Thread(target=yourfunc).start()def main():    # spawn whatever processes here to target threader