From 67ca0b90e511fe39c76ed17d2b20a9bba7e4f253 Mon Sep 17 00:00:00 2001 From: Brian Guarraci Date: Sun, 7 Jan 2024 08:01:48 -0700 Subject: [PATCH] add more multiprocessing examples --- examples/python/multiprocessing/callback.kg | 7 +++++++ examples/python/multiprocessing/callback.py | 21 +++++++++++++++++++ examples/python/multiprocessing/pool.kg | 2 +- examples/python/multiprocessing/worker/par.kg | 5 +++++ examples/python/multiprocessing/worker/par.py | 19 +++++++++++++++++ .../python/multiprocessing/worker/worker.kg | 2 ++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 examples/python/multiprocessing/callback.kg create mode 100644 examples/python/multiprocessing/callback.py create mode 100644 examples/python/multiprocessing/worker/par.kg create mode 100644 examples/python/multiprocessing/worker/par.py create mode 100644 examples/python/multiprocessing/worker/worker.kg diff --git a/examples/python/multiprocessing/callback.kg b/examples/python/multiprocessing/callback.kg new file mode 100644 index 0000000..e6356f3 --- /dev/null +++ b/examples/python/multiprocessing/callback.kg @@ -0,0 +1,7 @@ +.py("callback.py") + +cb::{.p(x);x*x} + +.p("running callback in parallel processes: ") +.p(runit(!10;cb)) + diff --git a/examples/python/multiprocessing/callback.py b/examples/python/multiprocessing/callback.py new file mode 100644 index 0000000..dfc843e --- /dev/null +++ b/examples/python/multiprocessing/callback.py @@ -0,0 +1,21 @@ +import multiprocessing +from klongpy import KlongInterpreter +from klongpy.core import KGFnWrapper + +def use_klongpy(args): + """ + This runs in a separate process so we need to instantiate a Klong interpreter. + The incoming function was serialized from the main process and marshaled to the parallel process. + We can run it in the new Klong context. + """ + n, fn = args + klong = KlongInterpreter() + fn = KGFnWrapper(klong, fn) + return fn(n) + + +def runit(numbers, fn): + """Apply the square function in parallel to a list of numbers.""" + with multiprocessing.Pool(1) as pool: + results = pool.map(use_klongpy, [(n,fn) for n in numbers]) + return results diff --git a/examples/python/multiprocessing/pool.kg b/examples/python/multiprocessing/pool.kg index 21594e7..e1f04d9 100644 --- a/examples/python/multiprocessing/pool.kg +++ b/examples/python/multiprocessing/pool.kg @@ -1,3 +1,3 @@ .py("pool.py") -runit(!10) +.d("parallel squared numbers: ");.p(runit(!10)) diff --git a/examples/python/multiprocessing/worker/par.kg b/examples/python/multiprocessing/worker/par.kg new file mode 100644 index 0000000..6764a47 --- /dev/null +++ b/examples/python/multiprocessing/worker/par.kg @@ -0,0 +1,5 @@ +.py("par.py") + +.p("running workers in parallel processes: ") +.p(runit(!10;"worker.kg";"worker")) + diff --git a/examples/python/multiprocessing/worker/par.py b/examples/python/multiprocessing/worker/par.py new file mode 100644 index 0000000..e9bfabf --- /dev/null +++ b/examples/python/multiprocessing/worker/par.py @@ -0,0 +1,19 @@ +import multiprocessing +from klongpy import KlongInterpreter +from klongpy.core import KGFnWrapper + +def use_klongpy(args): + """ + """ + n, fname, fn_name = args + klong = KlongInterpreter() + klong(f'.l("{fname}")') + return klong[fn_name](n) + + +def runit(numbers, fname, fn_name): + """ + """ + with multiprocessing.Pool(1) as pool: + results = pool.map(use_klongpy, [(n,fname,fn_name) for n in numbers]) + return results diff --git a/examples/python/multiprocessing/worker/worker.kg b/examples/python/multiprocessing/worker/worker.kg new file mode 100644 index 0000000..d43d7ed --- /dev/null +++ b/examples/python/multiprocessing/worker/worker.kg @@ -0,0 +1,2 @@ +worker::{.p(x);x*x} +