Kyle Sponable
Kyle Sponable

Reputation: 735

Threaded program wont run

I cant seem to get my fib_worker class to run

import multiprocessing


 class fib_Worker(object):
     def worker(self, num):
         """thread worker function"""
        print 'Worker:', num
        return
if __name__ == '__main__':
    jobs = []

    for i in range(5):
        p = multiprocessing.Process(target=Self.fib_Worker.worker, args=(i,))
        jobs.append(p)
        p.start()

Im getting this error:

TypeError: unbound method worker() must be called with fib_Worker instance as first argument (got int instance instead)

Ive tried calling it different ways and not sure whats wrong thanks

edit added to mhawkes suggestions

Upvotes: 2

Views: 37

Answers (1)

mhawke
mhawke

Reputation: 87054

What is Self.fib_Worker? You need to instantiate the class first:

p = multiprocessing.Process(target=fib_Worker().worker, args=(i,))

fib_Worker() creates an instance of class fib_Worker. Once you have that you can call its methods.

Also you need to add a self parameter to the worker() method:

    def worker(self, num):
        """thread worker function"""
        print 'Worker:', num
        return

Another option, since it holds no state, you can also create a single instance of the class and pass its method:

worker = fib_Worker().worker

for i in range(5):
    p = multiprocessing.Process(target=worker, args=(i,))
    jobs.append(p)
    p.start()

Upvotes: 1

Related Questions