Sunday, 20 December 2015

Python: multiprocessing: Custom process


You can create a custom process by sub classing multiprocessing.Process class and overriding run() method.
from multiprocessing import Process
import multiprocessing

class MyProcess(Process):
    def print_data(self):
        name = multiprocessing.current_process().name
        id = multiprocessing.current_process().pid
        print(name, id)
        print(name, "Processing finished")

    def run(self):
        self.print_data()


if __name__=="__main__":
    proc1 = MyProcess(name="Process1")
    proc2 = MyProcess(name="Process2")

    proc1.start()
    proc2.start()

    proc1.join()
    proc2.join()


Sample Output
Process1 32178
Process1 Processing finished
Process2 32179
Process2 Processing finished



Previous                                                 Next                                                 Home

No comments:

Post a Comment