阻塞调用而OSX Python中打开文件 [英] Blocking call while opening a file in python on osx

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

问题描述

我使用Python 2.7 OSX 10.9我要像他们的文字编辑默认文件揭幕战.txt文件,PDF首战.pdf文件等打开文件
由于文件被打开,应该可以挡住即我想打开一个文件,等到文件未关闭下一条指令的执行。我读<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/open.1.html\" rel=\"nofollow\">https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/open.1.html和如何在Mac OSX上<子进程打开文件/ A>并用 subprocess.call([开放, - W,文件名])。但在这种情况下,关闭即使在打开的文件,我必须手动强制退出从底座上的位置文件开门红。
因此,它会导致previous收盘打开的文件也。
 假设我有一个多标签的文本编辑器打开,我跑我的应用程序。然后,文本文件,将在标签上我已经运行的编辑器中打开。然后,我的计划将拒绝继续,直到我关闭所有我的标签,包括已经无关,与我的任务的人。那么,如何解决这个问题。当文件被打开,我通过看门狗处理文件中的变化的一个线程,原因为阻断

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

和,将关闭应用程序。结果
我真不明白它到底是什么,你想做的事,因为你的问题是相当严重的书面(不是我对自己的英语任何bettter)。但是,如果你想等待应用程序终止,你会怎么做:

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

但话又说回来,你必须给力:巧妙地关闭从被告席上的应用程序,因为再次.. OSX不关闭应用程序,因为你preSS的关闭按钮

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

这将:


  • 打开它打印有关OPEN的所有信息的I / O窥探,SEEK,作为文件名CLOSE等给予

  • 打开了您请求的文件
  • 的文本编辑
  • 如果关闭是在IO史努比present,我们关闭文本编辑

这只是一个想法,出了蓝色,但我解决了类似的问题与OSX这种方式。结果
还有<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/dtrace.1.html#//apple_ref/doc/man/1/dtrace\"相对=nofollow> DTrace的
,<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/iosnoop.1m.html\"相对=nofollow> iosnoop ,<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/execsnoop.1m.html\"相对=nofollow> 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天全站免登陆