Reputation: 541
I want to start a new process that will create some other object and then calls that objects loops_forever method.
I tried using Python's multiprocessing module as described in my previous SO question here: Python Multiprocessing calling object method
Here is what I tried:
import OtherService
from multiprocessing import Process
my_other_service = OtherService(address=ADDRESS)
my_other_process = Process(target=my_other_service.loops_forever)
my_other_process.start()
The problem with this solution is that the created object (OtherService) is not serializable and I get a "can't pickle _thread.lock object" error in the last line above.
How can I start a new process from my main class, and tell that process to create a new OtherService object (handing that process only the address parameter) and then tell that other process to call loops_forever?
Note that the solution doesnt have to use pythons multiprocessing, I will use whichever package makes most sense.
Upvotes: 0
Views: 1074
Reputation: 6430
You've got the answer in your question, in English. You just have to translate it into code.
tell that process to create a new OtherService object (handing that process only the address parameter) and then tell that other process to call loops_forever?
Create a simple new function, which will take an address, create an OtherService
object, and call its loops_forever()
method. This function is what's passed as the target
of the new process you create.
Like so:
def create_service_and_loop(address):
service = OtherService(address)
service.loops_forever()
proc = multiprocessing.Process(target=create_service_and_loop, args=(ADDRESS,))
proc.start()
Upvotes: 2