質問

I will start out by saying I have never done events and triggers in anything other than javascript.

I have a thread that runs and processes all the files in a folder. I want this thread to run everytime a new file writes to that folder. I thought events would be the way to do that, but I have no idea how to do that in python.

How do you tell a thread to throw or trigger an event? How does another thread pick this up?

sample thread:

def list_files_thread(dir):
    for filename in os.listdir(dir):
        print filename

thread.start_new_thread(list_files_thread, ('output',))

Edit: I am new to mutlithreading, so maybe I should use this instead?

class list_files_thread(threading.Thread):
    def __init__(self, directory):
        self.directory = directory
    def run(self):
        list_files(self.directory)

def list_files(directory_path):
    for filename in os.listdir(directory_path):
        print filename
役に立ちましたか?

解決

Well, the typical way to trigger asynchronous events is to use the signal module. As the documentation notes, signals cannot be used for inter-thread communication, so threads would be inappropriate in this case. Of course, you can also use threads using threading.Condition objects.

That being said, asynchronous events, threads, and signals are confusing and hard to implement correctly. Are you sure you want to reinvent the wheel? (You very well might be; I just like to check.) If not, watchdog provides the exact capabilities you require.

他のヒント

First of all, this is not a problem of python events it's more about Operative System Signals. You will need some way of monitoring the changes in the target folder. Since you are planning your code to be multiplatform I recommend you work with Qt specifically QtCore.QFileSystemWatcher.

Here are you have an aswer that can help you: How do I watch a file for changes?

Thanks for the great answers.

This is the code I ended up using:

def list_files(directory_path):
    print directory_path
    parent_dir = os.path.abspath(os.path.join(directory_path, os.pardir))
    print parent_dir
    for filename in os.listdir(parent_dir):
         print filename

class trigger_event_handler(FileSystemEventHandler):
    def on_created(self, event):
        super(trigger_event_handler, self).on_created(event)
        list_files(event.src_path)


new_event_handler = trigger_event_handler()
observer = Observer()
observer.schedule(new_event_handler, output_dir, recursive=False)
observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top