title: Assertion failure and NSInternalInconsistencyException when trying to run two functions simultaneously
body: <p>I've got two functions I'd like to run at the same time - show_video() and gesture_control(). The first reads in images from a camera stream and plots them using matplotlib. The second hooks up a few API's so that certain events are triggered based on objects detected in the video stream. I saw in a tutorial that I can run both functions simultaneously as follows:</p>
  
  <pre><code>if __name__ == '__main__':
      Thread(target = show_video).start()
      Thread(target = gesture_control).start()
  </code></pre>
  
  <p>But I get all kinds of errors...</p>
  
  <pre><code>2017-08-04 14:39:05.072 python[9512:693356] WARNING: nextEventMatchingMask should only be called from the Main Thread! This will throw an exception in the future.
  2017-08-04 14:39:05.072 python[9512:693356] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1349.25/Misc.subproj/NSUndoManager.m:363
  2017-08-04 14:39:05.075 python[9512:693356] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1349.25/Misc.subproj/NSUndoManager.m:363
  2017-08-04 14:39:05.077 python[9512:693356] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.'
  
  *** First throw call stack:
  (
      0   CoreFoundation                      0x00007fffa45580db __exceptionPreprocess + 171
      1   libobjc.A.dylib                     0x00007fffb91dfa2a objc_exception_throw + 48
      2   CoreFoundation                      0x00007fffa455cc32 +[NSException raise:format:arguments:] + 98
      3   Foundation                          0x00007fffa5fc5d50 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
      4   Foundation                          0x00007fffa5f503f3 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
      5   AppKit                              0x00007fffa1ff4047 -[NSApplication run] + 1200
  +50 more rows
  
  libc++abi.dylib: terminating with uncaught exception of type NSException
  </code></pre>