Reputation: 1037
I'm trying to work with different processes in Python, and I am having some difficulty getting the PID of a particular instance.
For example, I'm sending the mainCar instance in one class:
warehouse = Warehouse()
mainCar = Car().start()
warehouse.add(mainCar)
in the warehouse class, I'm reciving the mainCar variable and want to know its PID
How do I get the process id using the mainCar variable? I would be passing this variable to a different class and the process ID of this variable would be different to what os.getpid() give me.
Thanks in advance!
Upvotes: 0
Views: 2620
Reputation: 9633
I think you're fundamentally misunderstanding what's going on. Your question still doesn't make much sense, because objects have no notion of PID. Even if you used the multiprocessing module to spawn multiple processes and passed objects around with queues, there is no Python function that will tell you the PID of the process that created the object automatically.
You could add something like this to your classes to track originator PIDs:
class PID_Tracked(object):
def __init__(self)
self.originating_PID = os.getpid()
But unless you manually store this data, there is zero association between objects and the PID of the process that created them.
The one exception to all of this is if you're using the multiprocessing module. Some classes in that module will provide a PID to track the spawned processes. But nothing in your question indicates that you're using multiprocessing (at this time), so I've excluded a discussion of it.
Upvotes: 2