rumps icon indicating copy to clipboard operation
rumps copied to clipboard

Can't run in a thread?

Open mrmachine opened this issue 11 years ago • 3 comments

When I try to run the example menu bar app in a thread, I get an endless stream of this

2014-10-28 05:50:37.168 python[13952:248458] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /SourceCache/Foundation/Foundation-1151.16/Misc.subproj/NSUndoManager.m:340
2014-10-28 05:50:37.168 python[13952:248458] NSInternalInconsistencyException - +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2014-10-28 05:50:37.169 python[13952:248458] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /SourceCache/Foundation/Foundation-1151.16/Misc.subproj/NSUndoManager.m:340
2014-10-28 05:50:37.169 python[13952:248458] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2014-10-28 05:50:37.169 python[13952:248458] (
    0   CoreFoundation                      0x00007fff9b82764c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8d7bb6de objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff9b82742a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff99ddd5b9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   Foundation                          0x00007fff99d4efef +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 156
    5   AppKit                              0x00007fff8cc82015 -[NSApplication run] + 756
    6   _objc.so                            0x000000010c0c0907 ffi_call_unix64 + 79
    7   ???                                 0x000000010e0c25c0 0x0 + 4530644416
)

until I force quit. Here's the code:

class AppThread(Thread):
    def __init__(self, *args, **kwargs):
        super(AppThread, self).__init__(*args, **kwargs)
        self.start()

    def run(self):
        AwesomeStatusBarApp().run()

app_thread = AppThread()

Is this a limitation of pyobjc or a problem with rumps?

mrmachine avatar Oct 28 '14 06:10 mrmachine

Have the same problem. Need a solution

n3r0-ch avatar Jun 01 '15 16:06 n3r0-ch

From the error, it sounds like the underlying library expects the application to be in the main thread.

If you have work that needs to happen asynchronously, you can always spawn processes from the main GUI process.

davad avatar Jul 01 '15 15:07 davad

MacOS doesn't support GUI operations from non-main threads.

melvyn2 avatar Apr 21 '19 20:04 melvyn2