Domanda

I have a Python for .Net (2.7) script that subscribes to an event defined in a .NET assembly. When that event fires and my python-based event handler is called, is my handler executing in Python's main thread or in a worker thread?

...
def MyEventHandler(source, args):
    print 'Received message: ' + args.Message
...
dotNetObject.SomeEvent += MyEventHandler
...

I figured it must be a worker thread but when I put this line of code in the handler:

print threading._active

it only reported the main thread:

{8920: <_MainThread(MainThread, started 8920)>}

How can I tell what thread a given line of Python is executing in?

UPDATE: Okay, I found threading.current_thread(). Outside of my handler it returns:

<_MainThread(MainThread, started 7064)>

while inside my handler it returns

<_DummyThread(Dummy-1, started daemon 7916)>

So is a "DummyThread" a worker thread? Why didn't it show up in threading._active?

Thanks.

È stato utile?

Soluzione

DummyThreads are worker threads and show up in this call:

threading.enumerate()

So, yes, the event handler is executing in a worker thread.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top