Blocking call while opening a file in python on osx

可紊 提交于 2019-12-12 05:39:39

问题


I am using python 2.7 on osx 10.9 I want to open a file in their default file opener like TextEdit for .txt file, pdf opener for .pdf file etc. As file is opened, it should block i.e. I want to open a file and wait the execution of next instruction till file is not closed. I read https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/open.1.html and How to open a file in subprocess on mac osx and used subprocess.call(["open","-W", FileName]).But in this case, even when closing the opened file, I have to manually force quit the file opener from dock location. So, it causes closing of previous opened file also. Suppose I have a multi-tab text editor open and I run my application. Then text file will be opened in a tab on my already-running editor. Then my program will refuse to continue until I close all of my tabs, including the ones that had nothing to do with my task. So, how to resolve this. When file is opened, I am handling the changes in the file through watchdog in a thread, reason for blocking.

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ChangeHandler(FileSystemEventHandler):

    def on_any_event(self, event):
        myfunction(a, FileName, selectedFileName,b)

def filemonitor(FileName,tempLocation):

    while 1:
        global observer
        event_handler = ChangeHandler()
        observer = Observer()
        observer.schedule(event_handler, tempLocation, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()        

Thread(target=filemonitor,args=(FileName,tempLocation,)).start()    

subprocess.call(["open","-W", FileName])
observer.stop()

time.sleep(2)
os.remove(FileName) 

Opening file, blocking call. Then listening to changes. When file is closed, then remove the file


回答1:


Here's the deal with OSX. It doesn't close an application just because you "close" it. OSX is intended to speed up and make ease of everything you do, there for it leaves the application as is unless you do something new with it (open a new file which open up the application or you force close it).

You can do:

from subprocess import Popen
handle = Popen('open -W ' + FileName, shell=True)

# Do some stuff
handle,terminate()

And that would shut down the application.
I really didn't understand what exactly it is you want to do because your question is quite badly written (not that i'm any bettter with my English). But if you want to wait for the application to terminate, you'd do:

from time import sleep
while handle.poll() is None:
    sleep(0.025)
# do something after TextEditor has shut down

But then again, you'd have to force:ably close the application from the dock, because again.. OSX doesn't shut down the application just because you press the close button.

What you could do is something like:

from subprocess import Popen, STDOUT, PIPE

watchdog = Popen('iosnoop -f ' + FileName, shell=True, stdout=PIPE, stderr=STDOUT)
textfile = Popen('open -W ' + FileName, shell=True)

while watchdog.poll() is None:
    output = watchdog.stdout.readline()
    if 'close' in output.lower():
        break

textfile.terminate()
watchdog.terminate()
watchdog.stdout.close()

This would:

  • Open a I/O snoop which prints all information regarding OPEN, SEEK, CLOSE etc for the filename given
  • Open up the texteditor with the file you requested
  • If 'CLOSE' is present in the IO Snoop, we close the texteditor

It's just an idea out of the blue but i solved similar issues with OSX this way.
THere's also dtrace, iosnoop, execsnoop



来源:https://stackoverflow.com/questions/22076449/blocking-call-while-opening-a-file-in-python-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!