-
Notifications
You must be signed in to change notification settings - Fork 10
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
8 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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") | ||
|
||
fn::{[a];a::x*x;.p(a);a} | ||
|
||
.p("running callback in parallel processes: ") | ||
runit(!10;fn) | ||
|
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,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() |
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,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() |
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,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() |
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,3 @@ | ||
.py("pool.py") | ||
|
||
.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,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() | ||
|
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,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) |