Skip to content

Commit

Permalink
add more multiprocessing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Jan 7, 2024
1 parent 4c73131 commit 67ca0b9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/python/multiprocessing/callback.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.py("callback.py")

cb::{.p(x);x*x}

.p("running callback in parallel processes: ")
.p(runit(!10;cb))

21 changes: 21 additions & 0 deletions examples/python/multiprocessing/callback.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion examples/python/multiprocessing/pool.kg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.py("pool.py")

runit(!10)
.d("parallel squared numbers: ");.p(runit(!10))
5 changes: 5 additions & 0 deletions examples/python/multiprocessing/worker/par.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.py("par.py")

.p("running workers in parallel processes: ")
.p(runit(!10;"worker.kg";"worker"))

19 changes: 19 additions & 0 deletions examples/python/multiprocessing/worker/par.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions examples/python/multiprocessing/worker/worker.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
worker::{.p(x);x*x}

0 comments on commit 67ca0b9

Please sign in to comment.