在 osx 上用 python 打开文件时阻塞调用 [英] Blocking call while opening a file in python on osx

查看:26
本文介绍了在 osx 上用 python 打开文件时阻塞调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 osx 10.9 上使用 python 2.7 我想在他们的默认文件打开器中打开一个文件,比如用于 .txt 文件的 TextEdit、用于 .pdf 文件的 pdf 打开器等.当文件被打开时,它应该阻塞,即我想打开一个文件并等待下一条指令的执行,直到文件没有关闭.我阅读了 https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/open.1.html如何在 mac osx 上的子进程中打开文件 并使用 subprocess.call(["open","-W", FileName]).但在这种情况下,即使在关闭打开的文件时,我也必须从停靠位置手动强制退出文件打开器.因此,它也会导致关闭先前打开的文件.假设我打开了一个多选项卡文本编辑器并运行我的应用程序.然后文本文件将在我已经运行的编辑器的选项卡中打开.然后我的程序将拒绝继续,直到我关闭所有选项卡,包括与我的任务无关的选项卡.那么,如何解决这个问题.当文件打开时,我在一个线程中通过看门狗处理文件中的更改,阻塞的原因.

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

推荐答案

这是与 OSX 的交易.它不会因为您关闭"应用程序而关闭它.OSX 旨在加快和简化您所做的一切,因为它会使应用程序保持原样,除非您对其进行一些新操作(打开一个新文件以打开应用程序或强制关闭它).

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).

你可以这样做:

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

但话说回来,您必须强制:从 Dock 中巧妙地关闭应用程序,因为再次.. OSX 不会仅仅因为您按下关闭按钮就关闭应用程序.

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.

你可以做的是:

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()

这将:

  • 打开一个 I/O snoop,打印给定文件名的所有关于 OPEN、SEEK、CLOSE 等的信息
  • 使用您请求的文件打开文本编辑器
  • 如果 IO Snoop 中存在CLOSE",我们将关闭文本编辑器

这只是一个突然的想法,但我通过这种方式解决了 OSX 的类似问题.
还有 dtrace, iosnoop, execsnoop

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

这篇关于在 osx 上用 python 打开文件时阻塞调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆