Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use multithreading in flask #2961

Closed
tsiens opened this issue Jan 24, 2024 · 2 comments
Closed

How to use multithreading in flask #2961

tsiens opened this issue Jan 24, 2024 · 2 comments
Labels

Comments

@tsiens
Copy link

tsiens commented Jan 24, 2024

          > I think figured out what causes it to break. If you run flask with `threaded=False`, then it works. I think that indicates that the request is being handled by a new thread, but the new thread is no longer using the correct class loader. That is one of the thing's that's cautioned about in https://developer.android.com/training/articles/perf-jni#faq_FindClass:

You can get into trouble if you create a thread yourself (perhaps by calling pthread_create and then attaching it with AttachCurrentThread). Now there are no stack frames from your application. If you call FindClass from this thread, the JavaVM will start in the "system" class loader instead of the one associated with your application, so attempts to find app-specific classes will fail.

This seems to be exactly what's happening. A native thread is being created to handle each request in werkzeug. This new thread is attached to the JVM via a pyjnius monkey patch, but since it has a new stack frame you end up using the Java system class loader. I actually think this likely affects sdl2 apps as well since I don't think there's anything SDL can do to avoid this situation.

So, I think there are basically 2 solutions:

  1. Document that if you're using threads in your app, you have to resolve app classes before you run them.
  2. Resolve the classes in the JNI_OnLoad function. I think this is doable except that the build time constructed service classes would be tricky to handle. This also wouldn't affect the sdl2 bootstrap case since p4a uses it's JNI handling code.

For now I'm just going to make a PR that makes flask run non-threaded in the test app since it specifically tries to delay resolving the classes.

However, I still need to use Multithreading in the Flask,I am trying to use

from android.runnable import Runnable
Runnable(main)(*args, **kwargs)

But when the request calls the Runnable, the entire app will freeze and unable to perform any operation,Until the Runnableis completed
What should I do?

Originally posted by @tsiens in #2533 (comment)

@tsiens
Copy link
Author

tsiens commented Jan 24, 2024

in threaded=True or theard err:

jnius.jnius.JavaException: JVM exception occurred: Didn't find class "org.kivy.android.PythonActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]] java.lang.ClassNotFoundException

and i try autoclass in main.py first

from jnius import autoclass

autoclass('org.kivy.android.PythonActivity')
autoclass('org.jnius.NativeInvocationHandler')
autoclass('com.github.hbldh.bleak.PythonScanCallback')
autoclass('com.github.hbldh.bleak.PythonBluetoothGattCallback')
autoclass('com.github.hbldh.bleak.PythonScanCallback$Interface')
autoclass('com.github.hbldh.bleak.PythonBluetoothGattCallback$Interface')

then err:

jnius.jnius.JavaException: JVM exception occurred: Didn't find class "com.github.hbldh.bleak.PythonScanCallback$Interface" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]] java.lang.ClassNotFoundException

i think autoclass is success,but autoclass $Interface fail

Copy link

👋 @tsiens,
Sorry to hear you are having difficulties with Kivy's python-for-android; Kivy unites a number of different technologies, so building apps can be temperamental.
We try to use GitHub issues only to track work for developers to do to fix bugs and add new features to python-for-android.
However, this issue appears to be a support request. Please use our support channels to get help with the project.
If you're having trouble installing python-for-android, please see our quickstart guide.
If you're having trouble using python-for-android, please see our troubleshooting guide and FAQ.
Let us know if this comment was made in error, and we'll be happy to reopen the issue.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants