我正在尝试从我的pyobjc gui(osx状态栏中的菜单)向我的应用程序的主进程发送信号。具体来说,我正在运行一个包含在类中的gui,这是一个进程,我正在尝试通过管道从gui向主进程发送消息。
当我使用一种简单的方法将数据放入管道时,我的代码可以工作。消息传递给主进程,产生main process... recv(): foo
当我在子进程中启动gui并尝试将数据放入管道时,比如当我点击菜单选项'start'时,没有任何反应。主流程线永远不会被打印出来,据我所知,主流程被阻止了。
我假设这与pyobjc中的事件循环有关。我能做些什么来完成这项工作?如何将pyobjc代码作为子进程运行?
main.py
import sys
from multiprocessing import Process, Pipe
from userinterface import OSXstatusbaritem
def f2(pipe):
print "starting subprocess f2"
print pipe.send("foo")
pipe.close()
def main():
pipeUI, pipeServer = Pipe()
# p = Process(target=f2, args=(pipeUI,)) # <---------------------- This works
p = Process(target=OSXstatusbaritem.start(pipeUI), args=()) # <----This doesn't
p.start()
print "main process... recv():", pipeServer.recv()
p.join()
if __name__ == "__main__": sys.exit(main())
userinterface.py
import objc, re, os
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
from multiprocessing import Pipe
status_images = {'idle':'./ghost.png'}
class OSXstatusbaritem(NSObject):
images = {}
statusbar = None
state = 'idle'
@classmethod
def start(self, pipe):
self.pipe = pipe
self.start_time = NSDate.date()
app = NSApplication.sharedApplication()
delegate = self.alloc().init()
app.setDelegate_(delegate)
AppHelper.runEventLoop()
def applicationDidFinishLaunching_(self, notification):
statusbar = NSStatusBar.systemStatusBar()
# Create the statusbar item
self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
# Load all images
for i in status_images.keys():
self.images[i] = NSImage.alloc().initByReferencingFile_(status_images[i])
# Set initial image
self.statusitem.setImage_(self.images['idle'])
# self.statusitem.setAlternateImage_(self.images['highlight'])
# Let it highlight upon clicking
self.statusitem.setHighlightMode_(1)
# Set a tooltip
self.statusitem.setToolTip_('Sample app')
# Build a very simple menu
self.menu = NSMenu.alloc().init()
# Start and stop service
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Start Service', 'startService:', '')
self.menu.addItem_(menuitem)
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Stop Service', 'stopService:', '')
self.menu.addItem_(menuitem)
# Add a separator
menuitem = NSMenuItem.separatorItem()
self.menu.addItem_(menuitem)
# Terminate event
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
self.menu.addItem_(menuitem)
# Bind it to the status item
self.statusitem.setMenu_(self.menu)
# Get the timer going
self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(self.start_time, 5.0, self, 'tick:', None, True)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
self.timer.fire()
def tick_(self, notification):
print self.state
def startService_(self, notification):
self.pipe.send(["foobar", None])
print "starting service"
def stopService_(self, notification):
print "stopping service"
答案 0 :(得分:1)
您的代码在主进程(main.py)中创建GUI对象,然后在使用fork创建的子进程中使用该对象。大多数Apple的框架都不支持这一点。
此外,对OSXstatusbaritem.start的调用会在主进程中创建并运行eventloop。
您可以通过在子进程中创建GUI对象获得更多成功,但即使这样也不能保证工作(如果您不幸的是GUI框架已经初始化并在子进程中使用它时导致崩溃) :
p = Process(target=OSXstatusbaritem.start, args=(pipeUI,))
启动状态栏项的最安全方法是将其处理为使用子进程,以避免在不调用execv(2)的情况下创建新进程。 There was some talk on Python's tracker向多处理模块添加一个选项,以使用fork + exec启动新进程,但尚未导致提交。