Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:39:20+00:00 2026-06-07T06:39:20+00:00

The code below doesn’t work like I want it to. When I do svc.run()

  • 0

The code below doesn’t work like I want it to. When I do svc.run() the programs runs okay. Any changes I’ve made to my folder works file. But when I do svc.stop() it doesn’t exactly stop I think. Perhaps there must be a better way of doing the threading part…

import pyinotify
import threading
import time
import os
class fs_event_handler(pyinotify.ProcessEvent):
    def __init__(self, callback):
        self._callback = callback
    def process_IN_CREATE(self, e):
        self._callback(e.path, e.name, 'created')
    def process_IN_DELETE(self, e):
        self._callback(e.path, e.name, 'deleted')
    def process_IN_MODIFY(self, e):
        self._callback(e.path, e.name, 'modified')

class NotificationService():
    def __init__(self, path, callback):
        mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE \
            | pyinotify.IN_MODIFY
        w = pyinotify.WatchManager()
        self.__notifier = pyinotify.Notifier(w, fs_event_handler(callback))
        wdd = w.add_watch(path, mask, rec=True, auto_add=True)
        self.__loop = True
    def start(self):
        while self.__loop:
            self.__notifier.process_events()
            if self.__notifier.check_events():
                self.__notifier.read_events()
    def run(self):
        fm = FileMonitor(self)
        fm.start()
        print 'Service Running...'

    def stop(self):
        self.__notifier.stop()
        self.__loop = False

class FileMonitor(threading.Thread):
    def __init__(self, srvc):
        threading.Thread.__init__(self)
        self.__svc = srvc
    def run(self):
        self.__svc.start()
        print 'Service stopped'

def _callback(path, name, operation):
    zpad = lambda x: ''.join(['0', str(x)])[:2]
    ts = time.localtime()
    t = ':'.join(map(zpad, [ts.tm_hour, ts.tm_min, ts.tm_sec]))
    print t, ':', '%s was %s' % (os.path.join(path, name), operation)


p = '/home/deostroll/scripts/tmp'
svc = NotificationService(p, _callback)
svc.run()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:39:22+00:00Added an answer on June 7, 2026 at 6:39 am

    I’m not familiar with pyinorify, but it seems that self.__notifier.process_events() blocks so self.__loop doesn’t get read until some event happens.

    You can just use pyinotify.ThreadedNotifier instead:

    class NotificationService():
        def __init__(self, path, callback):
            self.path = path
            self.callback = callback
    
        def start(self):
            mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE \
                | pyinotify.IN_MODIFY
            w = pyinotify.WatchManager()
            wdd = w.add_watch(self.path, mask, rec=True, auto_add=True)
            self.__notifier = pyinotify.ThreadedNotifier(w, fs_event_handler(self.callback))
            self.__notifier.start()
            print 'Service Running...'
    
        def run(self):
            self.start()
    
        def stop(self):
            self.__notifier.stop()
            print 'Service Stopped'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below doesn't work on any browser. It's supposed to show an alert box.
I want to create a file but the below code doesn't create any file.
The code below doesn't seem to work or find anything on an array. I'm
This code below doesn't work correctly since my MFC program is in unicode circumstance.
Could anyone help me in assessing why the code below doesn't work. I'm using
The code below does not work but meant to illustrate what I want to
The code below doesn't work in the same behavior. The sequence of click event
It works perfectly from the admin site. But the code below doesn't work properly(some
The code below doesn't work, yet the type boolean exists and I used it
Why my code below doesn't trigger my JavaScriptInterface : I can't see any dialogbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.