8000 worker WIP · rnixx/python-for-android@70dc848 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 70dc848

Browse files
committed
worker WIP
1 parent 6a81bfb commit 70dc848

File tree

5 files changed

+149
-37
lines changed

5 files changed

+149
-37
lines changed

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ def make_package(args):
478478
'Worker.tmpl.java',
479479
worker_target_path,
480480
name=name,
481+
entrypoint=entrypoint,
481482
args=args,
482483
)
483484

pythonforandroid/bootstraps/common/build/jni/application/src/start.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,48 @@ JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart(
399399
main(1, argv);
400400
}
401401

402+
#if defined(BOOTSTRAP_NAME_SERVICELIBRARY)
403+
JNIEXPORT void JNICALL Java_org_kivy_android_PythonWorker_nativeStart(
404+
JNIEnv *env,
405+
jobject thiz,
406+
jstring j_android_private,
407+
jstring j_android_argument,
408+
jstring j_worker_entrypoint,
409+
jstring j_python_name,
410+
jstring j_python_home,
411+
jstring j_python_path) {
412+
jboolean iscopy;
413+
const char *android_private =
414+
(*env)->GetStringUTFChars(env, j_android_private, &iscopy);
415+
const char *android_argument =
416+
(*env)->GetStringUTFChars(env, j_android_argument, &iscopy);
417+
const char *worker_entrypoint =
418+
(*env)->GetStringUTFChars(env, j_worker_entrypoint, &iscopy);
419+
const char *python_name =
420+
(*env)->GetStringUTFChars(env, j_python_name, &iscopy);
421+
const char *python_home =
422+
(*env)->GetStringUTFChars(env, j_python_home, &iscopy);
423+
const char *python_path =
424+
(*env)->GetStringUTFChars(env, j_python_path, &iscopy);
425+
426+
setenv("ANDROID_PRIVATE", android_private, 1);
427+
setenv("ANDROID_ARGUMENT", android_argument, 1);
428+
setenv("ANDROID_APP_PATH", android_argument, 1);
429+
setenv("ANDROID_ENTRYPOINT", worker_entrypoint, 1);
430+
setenv("PYTHONOPTIMIZE", "2", 1);
431+
setenv("PYTHON_NAME", python_name, 1);
432+
setenv("PYTHONHOME", python_home, 1);
433+
setenv("PYTHONPATH", python_path, 1);
434+
setenv("P4A_BOOTSTRAP", bootstrap_name, 1);
435+
436+
char *argv[] = {"."};
437+
/* ANDROID_ARGUMENT points to service subdir,
438+
* so main() will run main.py from this dir
439+
*/
440+
main(1, argv);
441+
}
442+
#endif
443+
402444
#if defined(BOOTSTRAP_NAME_WEBVIEW) || defined(BOOTSTRAP_NAME_SERVICEONLY)
403445
// Webview and service_only uses some more functions:
404446

pythonforandroid/bootstraps/service_library/build/jni/application/src/bootstrap_name.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#define BOOTSTRAP_NAME_LIBRARY
3+
#define BOOTSTRAP_NAME_SERVICELIBRARY
34
#define BOOTSTRAP_USES_NO_SDL_HEADERS
45

56
const char bootstrap_name[] = "service_library";
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.kivy.android;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
import androidx.annotation.NonNull;
6+
import androidx.concurrent.futures.CallbackToFutureAdapter.Completer;
7+
import androidx.concurrent.futures.CallbackToFutureAdapter;
8+
import androidx.work.ListenableWorker.Result;
9+
import androidx.work.ListenableWorker;
10+
import androidx.work.Worker;
11+
import androidx.work.WorkerParameters;
12+
import com.google.common.util.concurrent.ListenableFuture;
13+
import java.io.File;
14+
15+
public class PythonWorker extends ListenableWorker implements Runnable {
16+
// Completer for worker notification
17+
private Completer workCompleter = null;
18+
19+
// Thread for Python code
20+
private Thread pythonThread = null;
21+
22+
// Application root directory
23+
private String appRoot;
24+
25+
// Python environment variables
26+
private String androidPrivate;
27+
private String androidArgument;
28+
private String pythonName;
29+
private String pythonHome;
30+
private String pythonPath;
31+
private String workerEntrypoint;
32+
33+
public PythonWorker(
34+
@NonNull Context context,
35+
@NonNull WorkerParameters params) {
36+
super(context, params);
37+
appRoot = context.getFilesDir().getAbsolutePath() + "/app";
38+
androidPrivate = appRoot;
39+
androidArgument = appRoot;
40+
pythonHome = appRoot;
41+
pythonPath = appRoot + ":" + appRoot + "/lib";
42+
}
43+
44+
public void setPythonName(String value) {
45+
pythonName = value;
46+
}
47+
48+
public void setWorkerEntrypoint(String value) {
49+
workerEntrypoint = value;
50+
}
51+
52+
@NonNull
53+
@Override
54+
public ListenableFuture<Result> startWork() {
55+
return CallbackToFutureAdapter.getFuture(completer -> {
56+
workCompleter = completer;
57+
58+
pythonThread = new Thread(this);
59+
pythonThread.start();
60+
61+
String msg = "PythonWorker started";
62+
Log.d("python worker", msg);
63+
return msg;
64+
});
65+
}
66+
67+
@Override
68+
public void run() {
69+
File app_root_file = new File(appRoot);
70+
71+
PythonUtil.loadLibraries(
72+
app_root_file,
73+
new File(getApplicationContext().getApplicationInfo().nativeLibraryDir)
74+
);
75+
76+
Log.d("python worker", "androidPrivate: " + androidPrivate);
77+
Log.d("python worker", "androidArgument: " + androidArgument);
78+
Log.d("python worker", "workerEntrypoint: " + workerEntrypoint);
79+
Log.d("python worker", "pythonName: " + pythonName);
80+
Log.d("python worker", "pythonHome: " + pythonHome);
81+
Log.d("python worker", "pythonPath: " + pythonPath);
82+
83+
nativeStart(
84+
androidPrivate, androidArgument,
85+
workerEntrypoint, pythonName,
86+
pythonHome, pythonPath
87+
);
88+
89+
workCompleter.set(Result.success());
90+
Log.d("python worker", "ThreadWorker Thread terminating");
91+
}
92+
93+
// Native part
94+
public static native void nativeStart(
95+
String androidPrivate, String androidArgument,
96+
String workerEntrypoint, String pythonName,
97+
String pythonHome, String pythonPath
98+
);
99+
}
Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,20 @@
11
package {{ args.package }};
22

33
import android.content.Context;
4-
import android.util.Log;
54
import androidx.annotation.NonNull;
6-
import androidx.concurrent.futures.CallbackToFutureAdapter.Completer;
7-
import androidx.concurrent.futures.CallbackToFutureAdapter;
8-
import androidx.work.ListenableWorker.Result;
9-
import androidx.work.ListenableWorker;
10-
import androidx.work.Worker;
115
import androidx.work.WorkerParameters;
12-
import com.google.common.util.concurrent.ListenableFuture;
6+
import org.kivy.android.PythonWorker;
7+
import org.kivy.android.PythonActivity;
138

149

15-
public class {{ name|capitalize }}Worker extends ListenableWorker implements Runnable {
16-
Thread pythonThread = null;
17-
Completer workCompleter = null;
10+
public class {{ name|capitalize }}Worker extends PythonWorker {
1811

1912
public {{ name|capitalize }}Worker (
2013
@NonNull Context context,
2114
@NonNull WorkerParameters params) {
2215
super(context, params);
23-
}
24-
25-
@NonNull
26-
@Override
27-
public ListenableFuture<Result> startWork() {
28-
return CallbackToFutureAdapter.getFuture(completer -> {
29-
workCompleter = completer;
30-
31-
pythonThread = new Thread(this);
32-
pythonThread.start();
33-
34-
String msg = "{{ name|capitalize }}Worker started";
35-
Log.d("Python Worker", msg);
36-
return msg;
37-
});
38-
}
39-
40-
@Override
41-
public void run() {
42-
Log.d("Python Worker", "I am the god of hell fire. And it bring you FIRE");
43-
44-
try {
45-
Thread.sleep(2000);
46-
} catch (InterruptedException e) {}
47-
48-
workCompleter.set(Result.success());
49-
Log.d("Python Worker", "{{ name|capitalize }}Worker Thread terminating");
16+
setPythonName("{{ name }}");
17+
// setServiceEntrypoint("{{ entrypoint }}");
18+
setWorkerEntrypoint("worker.py");
5019
}
5120
}

0 commit comments

Comments
 (0)
0