Check whether file has been written successfully [SOLVED]

Hey guys!

I need help with my moviefileout operator. Indeed, I’m writing a video file to disk, that I need to load immediately using a moviefilein. Yes, I need to write it down on the disk before reloading it.

The issue is that sometimes the moviefilein loads the movie before moviefileout has finished writing it completely. How can I make sure to avoid this?

Is there a callback function to ensure moviefileout finished writing?
Or is there a python function (“open” seems not to make the deal) to check whether the file is valid?

Thanks a lot for your input guys!

1 Like

How are you controlling your movie file out? Is this scripted or CHOPs?

I typically use a timer CHOP for writing movies to disk. You could use the done channel with a delay CHOP to offset your loading of the movie file TOP. Does that help?

You might also look at this thread as it may give you some ideas worth exploring:

viewtopic.php?f=27&t=8995&hilit=circular

Hey! I just dealt with some of this myself - you might want to kick off a python watchdog process in a standalone instance of Python and have it phone back to a listener in TD when it sees event.type == ‘modified’. Here’s an example to get you going:

[code]
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from watchdog.events import FileSystemEventHandler

WATCHPATH = ‘somepath/path’
FILETYPE = ‘*.mp4’

class Watcher:
def init(self):
self.observer = Observer()

def run(self):
	logging.info('WATCHER STARTED')
    event_handler = Handler(patterns=[FILETYPE])
    self.observer.schedule(event_handler, WATCHPATH, recursive=False)
    self.observer.start()
    try:
        while True:
            time.sleep(5)
    except:
        self.observer.stop()
        print("WATCHER STOPPED")

    self.observer.join()

class Handler(PatternMatchingEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None

    elif event.event_type == 'modified':
        # Take any action here when a file is modified or when it's finished writing to disk
        print("Received modified event - %s." % event.src_path)

    elif event.event_type == 'created':
        # Taken any action here when a file is created.
        print("Received created event - %s." % event.src_path)

    else:
    	print('Received other event: {} {}'.format(event.event_type, event.src_path))

if name == ‘main’:
w = Watcher()
w.run()[/code]

… hope that helps!

Noah, that looks like a very cool library.
Does it work reliably on Windows?
Thanks for the tip, I’ll try Watchdog.

Oh dip I’ve been using it on Mac, Idz - I don’t know about windows and I’m on a build right now so I can’t test. LMK what happens?

Hey guys!

Thanks a mil for your replies! Very helpful!

A timer is not the solution I prefer, as it is not a guarantee that my file is completely written, nor that I spend too much time waiting for it to complete even if it might be completed already.

The watchdog solution is neat! Only thing is that I need to run a parallel task for it! But otherwise, very good! And moreover, it gave me the idea of using the os.path.getmtime(“test.mp4”)) function, to check if my file is still being modified (and thus the file modified time is still changing). I’m still investigating this solution…

Another solution which came to my mind this morning was to use the warning message of the moviefilein operator: if the moviefilein operator has a “warning” message, I try to “reload” my file till there is no “warning” message anymore. It sounds like the perfect solution for me! The only question is:

How do I get access to the warning message of the moviefilein operator?

I tried with “op(‘/project1/Monitor/element0/moviefilein1’).warnings” but I get a “<built-in method warnings of td.moviefileinTOP object at 0x11cd330a8>”. Is there a way to count or get the error message/code of an operator?

Again, thanks a mil guys!

Hey hey!

I finally could find a perfectly working solution thanks to all your feedbacks. So here it is:

  1. I use an Error Dat to monitor my errors and filter them for what I need.
  2. When I get a new error, I start a timer (loops in Python will freeze TD)
  3. Once the timer has finished, I pulse the reload of my file.
  4. If I get an error, it automatically loops back to step 2).
  5. If no error, I’m super happy and continue my workflow.

Thanks!

Nice!

You could also considering using the python threading library to spawn a ‘followup’ thread to take care of this task for you without blocking the main thread, although watchdog is threaded itself and shouldn’t be blocking either.