0

I was just wondering how to get the name of a pyinotify.ThreadedNotifier calling my EventHandler.

pyinotify.ThreadedNotifier automatically get's a name like "Thread-1", "Thread-2", and so on.

import pyinotify

class EventHandler(pyinotify.ProcessEvent):
    def process_default(self, event):
        print "TRIGGER:", event.pathname

# Thread #1
wm1 = pyinotify.WatchManager()
notifier1 = pyinotify.ThreadedNotifier(wm1, EventHandler())
notifier1.start()
print notifier1.getName()
wm1.add_watch('/tmp/a', pyinotify.ALL_EVENTS, rec=True, auto_add=True)

# Thread #2
wm2 = pyinotify.WatchManager()
notifier2 = pyinotify.ThreadedNotifier(wm2, EventHandler())
notifier2.start()
print notifier2.getName()
wm2.add_watch('/tmp/b', pyinotify.ALL_EVENTS, rec=False, auto_add=False)

I can get the name of the notifier with the getName() function, but I have no idea how to find out which of them has called my EventHandler().

Is there a way to find out ?

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • Since python can only handle one thread at a time, isn't it safe to assume which one of them has called it? That is, the first one you executed. – Griffin Oct 29 '11 at 15:02
  • 2
    Just because only one thread is executing at any moment (thanks to the [Global Interpreter Lock, or GIL](http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/)) doesn't mean that the threads run serially or even deterministically. Thread scheduling is handled by the OS and threads are preempted every 100 byte code instructions. Thus you can *still* have race conditions despite the GIL. – Nathan Oct 29 '11 at 15:38

0 Answers0