Skip to content

Commit

Permalink
add threading examples
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Jan 8, 2024
1 parent 52f14bf commit cf7ae9e
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 0 deletions.
File renamed without changes.
7 changes: 7 additions & 0 deletions examples/python/threading/callback.kg
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)

16 changes: 16 additions & 0 deletions examples/python/threading/callback.py
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()
14 changes: 14 additions & 0 deletions examples/python/threading/callback_async.kg
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()
24 changes: 24 additions & 0 deletions examples/python/threading/callback_async.py
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()
3 changes: 3 additions & 0 deletions examples/python/threading/pool.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.py("pool.py")

.d("parallel squared numbers: ");.p(runit(!10))
12 changes: 12 additions & 0 deletions examples/python/threading/pool.py
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()

11 changes: 11 additions & 0 deletions examples/python/threading/pool_async.kg
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)

0 comments on commit cf7ae9e

Please sign in to comment.