From b5a1ecb7ec409c37e955acd6a12eb1d75ba3d1ab Mon Sep 17 00:00:00 2001 From: Akshay Arora Date: Wed, 26 Oct 2016 20:42:55 +0530 Subject: [PATCH] Detach only on android --- jnius/__init__.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/jnius/__init__.py b/jnius/__init__.py index 11c2414..5f7b7a6 100644 --- a/jnius/__init__.py +++ b/jnius/__init__.py @@ -39,11 +39,16 @@ PythonJavaClass = PythonJavaClass_ # from https://gist.github.com/tito/09c42fb4767721dc323d -import threading -orig_thread_run = threading.Thread.run -def thread_check_run(*args, **kwargs): - try: - return orig_thread_run(*args, **kwargs) - finally: - jnius.detach() -threading.Thread.run = thread_check_run +if "ANDROID_ARGUMENT" in os.environ: + # on android, catch all exception to ensure about a jnius.detach + import threading + import jnius + orig_thread_run = threading.Thread.run + + def jnius_thread_hook(*args, **kwargs): + try: + return orig_thread_run(*args, **kwargs) + finally: + jnius.detach() + + threading.Thread.run = jnius_thread_hook