-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
worker::{.p(x);x*x} | ||
|