threads convert to daemon

This commit is contained in:
Volkan Şahin 2016-08-02 18:16:24 +03:00
parent f13482c515
commit a5c864d33f
3 changed files with 12 additions and 38 deletions

View file

@ -14,12 +14,10 @@ class FileEventHandler(FileSystemEventHandler):
def process(self, event): def process(self, event):
if event.src_path != self.path[:-1]: if event.src_path != self.path[:-1]:
print('event_type:'+str(event.event_type))
if event.event_type in ('created', 'modified', 'moved'): if event.event_type in ('created', 'modified', 'moved'):
plu_path = event.src_path plu_path = event.src_path
if event.event_type == 'moved': if event.event_type == 'moved':
plu_path = event.dest_path plu_path = event.dest_path
print('plu_path'+str(plu_path))
result = Commander().set_event([None, 'load', '-p', plu_path.replace(self.path, '')]) result = Commander().set_event([None, 'load', '-p', plu_path.replace(self.path, '')])
if result is True and System.Ahenk.is_running() is True: if result is True and System.Ahenk.is_running() is True:
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM) os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)

View file

@ -1,43 +1,20 @@
import os import threading
import signal
import time import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer from watchdog.observers import Observer
from base.command.commander import Commander from base.plugin.file_handler import FileEventHandler
from base.system.system import System
class FileEventHandler(FileSystemEventHandler): class PluginInstallListener(threading.Thread):
def __init__(self, plugin_path): def __init__(self, plugin_path):
threading.Thread.__init__(self)
self.path = plugin_path self.path = plugin_path
def process(self, event): def run(self):
if event.src_path != self.path[:-1]:
if event.event_type in ('created', 'modified', 'moved'):
plu_path = event.src_path
if event.event_type == 'moved':
plu_path = event.dest_path
result = Commander().set_event([None, 'load', '-p', plu_path.replace(self.path, '')])
if result is True and System.Ahenk.is_running() is True:
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
elif event.event_type == 'deleted':
result = Commander().set_event([None, 'remove', '-p', event.src_path.replace(self.path, '')])
if result is True and System.Ahenk.is_running() is True:
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
def on_any_event(self, event):
if event.is_directory:
self.process(event)
class PluginInstallListener:
def listen(self, path):
observer = Observer() observer = Observer()
event_handler = FileEventHandler(path) event_handler = FileEventHandler(self.path)
observer.schedule(event_handler, path, recursive=False) observer.schedule(event_handler, self.path, recursive=False)
observer.start() observer.start()
try: try:
while True: while True:

View file

@ -4,7 +4,6 @@
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com> # Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import imp import imp
import os import os
from multiprocessing import Process
from base.Scope import Scope from base.Scope import Scope
from base.model.PluginBean import PluginBean from base.model.PluginBean import PluginBean
@ -34,7 +33,8 @@ class PluginManager(object):
self.plugins = [] self.plugins = []
self.pluginQueueDict = dict() self.pluginQueueDict = dict()
self.listener = self.install_listener() # self.listener = \
self.install_listener()
self.delayed_profiles = {} self.delayed_profiles = {}
self.delayed_tasks = {} self.delayed_tasks = {}
@ -265,10 +265,9 @@ class PluginManager(object):
return None return None
def install_listener(self): def install_listener(self):
listener = PluginInstallListener() listener = PluginInstallListener(System.Ahenk.plugins_path())
thread = Process(target=listener.listen, args=(System.Ahenk.plugins_path(),)) listener.setDaemon(True)
thread.start() listener.start()
return thread
def is_plugin_loaded(self, plugin_name): def is_plugin_loaded(self, plugin_name):
try: try: