From cf7ae9ef75697f9e69cf14fcfcbd32f4cbae749f Mon Sep 17 00:00:00 2001 From: Brian Guarraci Date: Mon, 8 Jan 2024 16:32:33 -0700 Subject: [PATCH] add threading examples --- .../{async.kg => pool_async.kg} | 0 examples/python/threading/callback.kg | 7 ++++++ examples/python/threading/callback.py | 16 +++++++++++++ examples/python/threading/callback_async.kg | 14 +++++++++++ examples/python/threading/callback_async.py | 24 +++++++++++++++++++ examples/python/threading/pool.kg | 3 +++ examples/python/threading/pool.py | 12 ++++++++++ examples/python/threading/pool_async.kg | 11 +++++++++ 8 files changed, 87 insertions(+) rename examples/python/multiprocessing/{async.kg => pool_async.kg} (100%) create mode 100644 examples/python/threading/callback.kg create mode 100644 examples/python/threading/callback.py create mode 100644 examples/python/threading/callback_async.kg create mode 100644 examples/python/threading/callback_async.py create mode 100644 examples/python/threading/pool.kg create mode 100644 examples/python/threading/pool.py create mode 100644 examples/python/threading/pool_async.kg diff --git a/examples/python/multiprocessing/async.kg b/examples/python/multiprocessing/pool_async.kg similarity index 100% rename from examples/python/multiprocessing/async.kg rename to examples/python/multiprocessing/pool_async.kg diff --git a/examples/python/threading/callback.kg b/examples/python/threading/callback.kg new file mode 100644 index 0000000..d567f7a --- /dev/null +++ b/examples/python/threading/callback.kg @@ -0,0 +1,7 @@ +.py("callback.py") + +fn::{[a];a::x*x;.p(a);a} + +.p("running callback in parallel processes: ") +runit(!10;fn) + diff --git a/examples/python/threading/callback.py b/examples/python/threading/callback.py new file mode 100644 index 0000000..2d9d280 --- /dev/null +++ b/examples/python/threading/callback.py @@ -0,0 +1,16 @@ +from multiprocessing.pool import ThreadPool +from klongpy.core import KGFnWrapper +import time + +def use_klongpy(numbers, fn): + """ + This runs in the same process as the KlongInterpreter, so we can use the fn directly. + """ + return fn(numbers) + + +def runit(klong, numbers, fn): + """Apply the square function in parallel to a list of numbers.""" + fn = KGFnWrapper(klong, fn) # TODO: this should already come wrapped from the interpreter + with ThreadPool() as pool: + return pool.apply_async(use_klongpy, (numbers, fn,)).get() diff --git a/examples/python/threading/callback_async.kg b/examples/python/threading/callback_async.kg new file mode 100644 index 0000000..998ff45 --- /dev/null +++ b/examples/python/threading/callback_async.kg @@ -0,0 +1,14 @@ +.py("callback_async.py") + +cb::{.d("callback called: ");.p(x)} +fn::{x*x} + +t::0 +.tcb::{.d("counter: ");.p(t);t::t+1} +th::.timer("counter";0.25;tcb) + +.p("running callback in parallel processes: ") +runit(!10;fn;cb) +.d("doing other work: ");.p(24*24) + +wait() \ No newline at end of file diff --git a/examples/python/threading/callback_async.py b/examples/python/threading/callback_async.py new file mode 100644 index 0000000..af18a41 --- /dev/null +++ b/examples/python/threading/callback_async.py @@ -0,0 +1,24 @@ +from multiprocessing.pool import ThreadPool +from klongpy.core import KGFnWrapper +import time + +def use_klongpy(numbers, fn): + """ + This runs in the same process as the KlongInterpreter, so we can use the fn directly. + """ + time.sleep(1) + print("done sleeping") + return fn(numbers) + +pool = ThreadPool() + +def runit(klong, numbers, fn, cb): + """Apply the square function in parallel to a list of numbers.""" + fn = KGFnWrapper(klong, fn) # TODO: this should already come wrapped from the interpreter + cb = KGFnWrapper(klong, cb) + # with ThreadPool() as pool: + return pool.apply_async(use_klongpy, (numbers, fn,), callback=cb) + +def wait(): + pool.close() + pool.join() diff --git a/examples/python/threading/pool.kg b/examples/python/threading/pool.kg new file mode 100644 index 0000000..e1f04d9 --- /dev/null +++ b/examples/python/threading/pool.kg @@ -0,0 +1,3 @@ +.py("pool.py") + +.d("parallel squared numbers: ");.p(runit(!10)) diff --git a/examples/python/threading/pool.py b/examples/python/threading/pool.py new file mode 100644 index 0000000..21c2352 --- /dev/null +++ b/examples/python/threading/pool.py @@ -0,0 +1,12 @@ +from multiprocessing.pool import ThreadPool + +def square(numbers): + """Function to square a number.""" + return [n * n for n in numbers] + + +def runit(numbers): + """Apply the square function in parallel to a list of numbers.""" + with ThreadPool() as pool: + return pool.apply_async(square, (numbers,)).get() + diff --git a/examples/python/threading/pool_async.kg b/examples/python/threading/pool_async.kg new file mode 100644 index 0000000..51a2065 --- /dev/null +++ b/examples/python/threading/pool_async.kg @@ -0,0 +1,11 @@ +.py("pool.py") + +.p("async parallel squared numbers: "); + +:" create an async wrapper for the parallel task " +cb::{.d("done: ");.p(x)} +fn::{x*x} +arunit::.async(runit; cb) + +arunit(!10;fn) +.d("doing other work: ");.p(24*24)