From b8853664878a4d48bfdfc0dc978e8dc6b267e1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sun, 17 Dec 2023 21:14:53 -0300 Subject: [PATCH 01/25] adding base python bindings --- python/pool_day.py | 26 ++++++++++++++++++++++++++ python/pool_day/__init__.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 python/pool_day.py create mode 100644 python/pool_day/__init__.py diff --git a/python/pool_day.py b/python/pool_day.py new file mode 100644 index 0000000..c4ead80 --- /dev/null +++ b/python/pool_day.py @@ -0,0 +1,26 @@ +from pool_day import _lib_handle + +from ctypes import byref, c_uint8 + + +def create_pool(size): + return _lib_handle.create_pool(c_uint8(size)) + + +def destroy_pool(pool): + return _lib_handle.destroy_pool(byref(pool)) + + +def abort_tasks(pool): + return _lib_handle.abort_tasks(pool) + + +def main(): + pool = create_pool(5) + + print('ret =', abort_tasks(pool)) + print('ret =', destroy_pool(pool)) + + +if __name__ == "__main__": + main() diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py new file mode 100644 index 0000000..7ba6474 --- /dev/null +++ b/python/pool_day/__init__.py @@ -0,0 +1,35 @@ +from ctypes import ( + CDLL, + POINTER, + Structure, + c_int, + c_uint8, + c_void_p +) + + +class PoolDay(Structure): + pass + + +class Task(Structure): + pass + + +_lib_handle = CDLL('../build/libpool-day.so') + +# create_pool +_lib_handle.create_pool.argtypes = [c_uint8] +_lib_handle.create_pool.restype = POINTER(PoolDay) + +# destroy_pool +_lib_handle.destroy_pool.argtypes = [c_void_p] +_lib_handle.destroy_pool.restype = c_int + +# abort_tasks +_lib_handle.abort_tasks.argtypes = [POINTER(PoolDay)] +_lib_handle.abort_tasks.restype = c_int + +# enqueue_task +_lib_handle.enqueue_task.argtypes = [POINTER(PoolDay), POINTER(Task)] +_lib_handle.enqueue_task.restype = c_int From 850a8ce6334f8410db78262a4c010322cac3240f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Tue, 19 Dec 2023 02:10:35 -0300 Subject: [PATCH 02/25] python: adding create_task, idle_tasks and create tasks --- python/pool_day.py | 31 +++++++++++++++++++++++++++---- python/pool_day/__init__.py | 25 ++++++++++++++++++------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index c4ead80..1ecfb2d 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,8 +1,12 @@ -from pool_day import _lib_handle +from pool_day import _lib_handle, lib_cb from ctypes import byref, c_uint8 +def thread_cb(param): + print(param) + + def create_pool(size): return _lib_handle.create_pool(c_uint8(size)) @@ -11,15 +15,34 @@ def destroy_pool(pool): return _lib_handle.destroy_pool(byref(pool)) +def enqueue_task(pool, task): + return _lib_handle.enqueue_task(pool, task) + + +def create_task(cb, param): + return _lib_handle.create_task(lib_cb(cb), param) + + def abort_tasks(pool): return _lib_handle.abort_tasks(pool) +def idle_tasks(pool): + return _lib_handle.idle_tasks(pool) + + def main(): - pool = create_pool(5) + pool = create_pool(1) + + try: + t1 = create_task(thread_cb, 'hello 1') + + print('enqueue_task ret =', enqueue_task(pool, t1)) + print('idle_tasks ret =', idle_tasks(pool)) - print('ret =', abort_tasks(pool)) - print('ret =', destroy_pool(pool)) + input('') + except KeyboardInterrupt: + print('destroy_pool ret =', destroy_pool(pool)) if __name__ == "__main__": diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 7ba6474..16d208b 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,10 +1,11 @@ +from ctypes import CDLL as cdll +from ctypes import CFUNCTYPE as c_func_type +from ctypes import POINTER as c_pointer from ctypes import ( - CDLL, - POINTER, Structure, c_int, c_uint8, - c_void_p + c_void_p, ) @@ -16,20 +17,30 @@ class Task(Structure): pass -_lib_handle = CDLL('../build/libpool-day.so') +lib_cb = c_func_type(c_void_p, c_void_p) + +_lib_handle = cdll('../build/libpool-day.so') # create_pool _lib_handle.create_pool.argtypes = [c_uint8] -_lib_handle.create_pool.restype = POINTER(PoolDay) +_lib_handle.create_pool.restype = c_pointer(PoolDay) # destroy_pool _lib_handle.destroy_pool.argtypes = [c_void_p] _lib_handle.destroy_pool.restype = c_int # abort_tasks -_lib_handle.abort_tasks.argtypes = [POINTER(PoolDay)] +_lib_handle.abort_tasks.argtypes = [c_pointer(PoolDay)] _lib_handle.abort_tasks.restype = c_int # enqueue_task -_lib_handle.enqueue_task.argtypes = [POINTER(PoolDay), POINTER(Task)] +_lib_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] _lib_handle.enqueue_task.restype = c_int + +# idle_tasks +_lib_handle.idle_tasks.argtypes = [c_pointer(PoolDay)] +_lib_handle.idle_tasks.restype = c_uint8 + +# create_task +_lib_handle.create_task.argtypes = [c_void_p, c_void_p] +_lib_handle.create_task.restype = c_pointer(Task) From 09e9cda4e8f2d4475852eff870813d315272f590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Tue, 19 Dec 2023 23:35:17 -0300 Subject: [PATCH 03/25] calling correctly the thread callback --- python/pool_day.py | 34 ++++++++++++++++++---------------- python/pool_day/__init__.py | 3 --- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index 1ecfb2d..14ce71f 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,10 +1,11 @@ -from pool_day import _lib_handle, lib_cb - -from ctypes import byref, c_uint8 +from pool_day import _lib_handle +from ctypes import byref, c_void_p, c_uint8 +from ctypes import CFUNCTYPE as c_func_type +@c_func_type(c_void_p, c_void_p) def thread_cb(param): - print(param) + print('hello from python callback!!!') def create_pool(size): @@ -20,7 +21,7 @@ def enqueue_task(pool, task): def create_task(cb, param): - return _lib_handle.create_task(lib_cb(cb), param) + return _lib_handle.create_task(cb, param) def abort_tasks(pool): @@ -32,17 +33,18 @@ def idle_tasks(pool): def main(): - pool = create_pool(1) - - try: - t1 = create_task(thread_cb, 'hello 1') - - print('enqueue_task ret =', enqueue_task(pool, t1)) - print('idle_tasks ret =', idle_tasks(pool)) - - input('') - except KeyboardInterrupt: - print('destroy_pool ret =', destroy_pool(pool)) + pool = create_pool(2) + + t1 = create_task(thread_cb, 'hello 1') + t2 = create_task(thread_cb, 'hello 1') + t3 = create_task(thread_cb, 'hello 1') + + print('enqueue_task ret =', enqueue_task(pool, t1)) + print('enqueue_task ret =', enqueue_task(pool, t2)) + print('enqueue_task ret =', enqueue_task(pool, t3)) + print('idle_tasks ret =', idle_tasks(pool)) + input('') + print('destroy_pool ret =', destroy_pool(pool)) if __name__ == "__main__": diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 16d208b..7ce7d18 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,5 +1,4 @@ from ctypes import CDLL as cdll -from ctypes import CFUNCTYPE as c_func_type from ctypes import POINTER as c_pointer from ctypes import ( Structure, @@ -17,8 +16,6 @@ class Task(Structure): pass -lib_cb = c_func_type(c_void_p, c_void_p) - _lib_handle = cdll('../build/libpool-day.so') # create_pool From e85f31ef0368a54f22d4ec943c798b54bda1c23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Tue, 19 Dec 2023 23:43:50 -0300 Subject: [PATCH 04/25] adding 'pool_day_callback' decorator to define a pool_day callback --- python/pool_day.py | 22 +++++++++++----------- python/pool_day/__init__.py | 36 +++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index 14ce71f..6d0d705 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,43 +1,43 @@ -from pool_day import _lib_handle +from pool_day import _pd_handle, pool_day_callback from ctypes import byref, c_void_p, c_uint8 -from ctypes import CFUNCTYPE as c_func_type -@c_func_type(c_void_p, c_void_p) + +@pool_day_callback def thread_cb(param): print('hello from python callback!!!') def create_pool(size): - return _lib_handle.create_pool(c_uint8(size)) + return _pd_handle.create_pool(c_uint8(size)) def destroy_pool(pool): - return _lib_handle.destroy_pool(byref(pool)) + return _pd_handle.destroy_pool(byref(pool)) def enqueue_task(pool, task): - return _lib_handle.enqueue_task(pool, task) + return _pd_handle.enqueue_task(pool, task) def create_task(cb, param): - return _lib_handle.create_task(cb, param) + return _pd_handle.create_task(cb, param) def abort_tasks(pool): - return _lib_handle.abort_tasks(pool) + return _pd_handle.abort_tasks(pool) def idle_tasks(pool): - return _lib_handle.idle_tasks(pool) + return _pd_handle.idle_tasks(pool) def main(): pool = create_pool(2) t1 = create_task(thread_cb, 'hello 1') - t2 = create_task(thread_cb, 'hello 1') - t3 = create_task(thread_cb, 'hello 1') + t2 = create_task(thread_cb, 'hello 2') + t3 = create_task(thread_cb, 'hello 3') print('enqueue_task ret =', enqueue_task(pool, t1)) print('enqueue_task ret =', enqueue_task(pool, t2)) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 7ce7d18..1d399bb 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,5 +1,6 @@ from ctypes import CDLL as cdll from ctypes import POINTER as c_pointer +from ctypes import CFUNCTYPE as c_func_type from ctypes import ( Structure, c_int, @@ -16,28 +17,37 @@ class Task(Structure): pass -_lib_handle = cdll('../build/libpool-day.so') +def pool_day_callback(cb): + + @c_func_type(c_void_p, c_void_p) + def _cb(param): + cb(param) + + return _cb + + +_pd_handle = cdll('../build/libpool-day.so') # create_pool -_lib_handle.create_pool.argtypes = [c_uint8] -_lib_handle.create_pool.restype = c_pointer(PoolDay) +_pd_handle.create_pool.argtypes = [c_uint8] +_pd_handle.create_pool.restype = c_pointer(PoolDay) # destroy_pool -_lib_handle.destroy_pool.argtypes = [c_void_p] -_lib_handle.destroy_pool.restype = c_int +_pd_handle.destroy_pool.argtypes = [c_void_p] +_pd_handle.destroy_pool.restype = c_int # abort_tasks -_lib_handle.abort_tasks.argtypes = [c_pointer(PoolDay)] -_lib_handle.abort_tasks.restype = c_int +_pd_handle.abort_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.abort_tasks.restype = c_int # enqueue_task -_lib_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] -_lib_handle.enqueue_task.restype = c_int +_pd_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] +_pd_handle.enqueue_task.restype = c_int # idle_tasks -_lib_handle.idle_tasks.argtypes = [c_pointer(PoolDay)] -_lib_handle.idle_tasks.restype = c_uint8 +_pd_handle.idle_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.idle_tasks.restype = c_uint8 # create_task -_lib_handle.create_task.argtypes = [c_void_p, c_void_p] -_lib_handle.create_task.restype = c_pointer(Task) +_pd_handle.create_task.argtypes = [c_void_p, c_void_p] +_pd_handle.create_task.restype = c_pointer(Task) From 949595c57a12545ca4a41bad3417cf81b13a3e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Tue, 19 Dec 2023 23:47:46 -0300 Subject: [PATCH 05/25] removing unecessary imports --- python/pool_day.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index 6d0d705..a80e048 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,6 +1,6 @@ from pool_day import _pd_handle, pool_day_callback -from ctypes import byref, c_void_p, c_uint8 +from ctypes import byref @pool_day_callback @@ -9,7 +9,7 @@ def thread_cb(param): def create_pool(size): - return _pd_handle.create_pool(c_uint8(size)) + return _pd_handle.create_pool(size) def destroy_pool(pool): From 89ccb99499ea301b8ac7508155a3498d6798bf5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Tue, 19 Dec 2023 23:51:01 -0300 Subject: [PATCH 06/25] adding an ID to the thread callback example --- python/pool_day.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index a80e048..0d8300c 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,11 +1,14 @@ from pool_day import _pd_handle, pool_day_callback from ctypes import byref +from time import sleep @pool_day_callback def thread_cb(param): - print('hello from python callback!!!') + for i in range(1, 10): + print('thread %d: hello from python callback, i: %d' % (param, i)) + sleep(param) def create_pool(size): @@ -35,9 +38,9 @@ def idle_tasks(pool): def main(): pool = create_pool(2) - t1 = create_task(thread_cb, 'hello 1') - t2 = create_task(thread_cb, 'hello 2') - t3 = create_task(thread_cb, 'hello 3') + t1 = create_task(thread_cb, 1) + t2 = create_task(thread_cb, 2) + t3 = create_task(thread_cb, 3) print('enqueue_task ret =', enqueue_task(pool, t1)) print('enqueue_task ret =', enqueue_task(pool, t2)) From 099db07e7367b2194bd4072435bfa765ca32cdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Thu, 16 Oct 2025 23:28:48 -0300 Subject: [PATCH 07/25] python: making 'create_pool' function a contextmanager --- python/pool_day.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index 0d8300c..9699fd7 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,8 +1,9 @@ -from pool_day import _pd_handle, pool_day_callback - +from contextlib import contextmanager from ctypes import byref from time import sleep +from pool_day import _pd_handle, pool_day_callback + @pool_day_callback def thread_cb(param): @@ -11,8 +12,11 @@ def thread_cb(param): sleep(param) +@contextmanager def create_pool(size): - return _pd_handle.create_pool(size) + pool = _pd_handle.create_pool(size) + yield pool + _pd_handle.destroy_pool(byref(pool)) def destroy_pool(pool): From e7b5b2872ed4e6d328b77d095242512560c2c00b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Thu, 16 Oct 2025 23:32:56 -0300 Subject: [PATCH 08/25] python: updating example --- python/pool_day.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index 9699fd7..4355b9b 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -40,18 +40,16 @@ def idle_tasks(pool): def main(): - pool = create_pool(2) - - t1 = create_task(thread_cb, 1) - t2 = create_task(thread_cb, 2) - t3 = create_task(thread_cb, 3) - - print('enqueue_task ret =', enqueue_task(pool, t1)) - print('enqueue_task ret =', enqueue_task(pool, t2)) - print('enqueue_task ret =', enqueue_task(pool, t3)) - print('idle_tasks ret =', idle_tasks(pool)) - input('') - print('destroy_pool ret =', destroy_pool(pool)) + with create_pool(2) as pool: + t1 = create_task(thread_cb, 1) + t2 = create_task(thread_cb, 2) + t3 = create_task(thread_cb, 3) + + print('enqueue_task ret =', enqueue_task(pool, t1)) + print('enqueue_task ret =', enqueue_task(pool, t2)) + print('enqueue_task ret =', enqueue_task(pool, t3)) + print('idle_tasks ret =', idle_tasks(pool)) + input('') if __name__ == "__main__": From f4ace75ec54ae232b801f677d220024974b37f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 16:13:14 -0300 Subject: [PATCH 09/25] python: renaming idle_tasks function to queued_tasks --- python/pool_day.py | 6 +++--- python/pool_day/__init__.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/python/pool_day.py b/python/pool_day.py index 4355b9b..a806873 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -35,8 +35,8 @@ def abort_tasks(pool): return _pd_handle.abort_tasks(pool) -def idle_tasks(pool): - return _pd_handle.idle_tasks(pool) +def queued_tasks(pool): + return _pd_handle.queued_tasks(pool) def main(): @@ -48,7 +48,7 @@ def main(): print('enqueue_task ret =', enqueue_task(pool, t1)) print('enqueue_task ret =', enqueue_task(pool, t2)) print('enqueue_task ret =', enqueue_task(pool, t3)) - print('idle_tasks ret =', idle_tasks(pool)) + print('queued_tasks ret =', queued_tasks(pool)) input('') diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 1d399bb..9afe4c1 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -44,9 +44,9 @@ def _cb(param): _pd_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] _pd_handle.enqueue_task.restype = c_int -# idle_tasks -_pd_handle.idle_tasks.argtypes = [c_pointer(PoolDay)] -_pd_handle.idle_tasks.restype = c_uint8 +# queued_tasks +_pd_handle.queued_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.queued_tasks.restype = c_uint8 # create_task _pd_handle.create_task.argtypes = [c_void_p, c_void_p] From b5530a61fb1c077bd166d6025d0a0324b1232aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 16:16:54 -0300 Subject: [PATCH 10/25] python: moving sample code to 'samples' folder --- python/pool_day.py | 27 +-------------------------- samples/python/sample.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 samples/python/sample.py diff --git a/python/pool_day.py b/python/pool_day.py index a806873..6d22fea 100644 --- a/python/pool_day.py +++ b/python/pool_day.py @@ -1,15 +1,7 @@ from contextlib import contextmanager from ctypes import byref -from time import sleep -from pool_day import _pd_handle, pool_day_callback - - -@pool_day_callback -def thread_cb(param): - for i in range(1, 10): - print('thread %d: hello from python callback, i: %d' % (param, i)) - sleep(param) +from pool_day import _pd_handle @contextmanager @@ -37,20 +29,3 @@ def abort_tasks(pool): def queued_tasks(pool): return _pd_handle.queued_tasks(pool) - - -def main(): - with create_pool(2) as pool: - t1 = create_task(thread_cb, 1) - t2 = create_task(thread_cb, 2) - t3 = create_task(thread_cb, 3) - - print('enqueue_task ret =', enqueue_task(pool, t1)) - print('enqueue_task ret =', enqueue_task(pool, t2)) - print('enqueue_task ret =', enqueue_task(pool, t3)) - print('queued_tasks ret =', queued_tasks(pool)) - input('') - - -if __name__ == "__main__": - main() diff --git a/samples/python/sample.py b/samples/python/sample.py new file mode 100644 index 0000000..37cbd9f --- /dev/null +++ b/samples/python/sample.py @@ -0,0 +1,33 @@ +from time import sleep + +from pool_day import ( + create_pool, + create_task, + enqueue_task, + pool_day_callback, + queued_tasks +) + + +@pool_day_callback +def thread_cb(param): + for i in range(1, 10): + print('thread %d: hello from python callback, i: %d' % (param, i)) + sleep(param) + + +def main(): + with create_pool(2) as pool: + t1 = create_task(thread_cb, 1) + t2 = create_task(thread_cb, 2) + t3 = create_task(thread_cb, 3) + + print('enqueue_task ret =', enqueue_task(pool, t1)) + print('enqueue_task ret =', enqueue_task(pool, t2)) + print('enqueue_task ret =', enqueue_task(pool, t3)) + print('queued_tasks ret =', queued_tasks(pool)) + input('') + + +if __name__ == "__main__": + main() From 65f1d9e559693aa28f8dc9e7d7253012bf1643e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 16:21:02 -0300 Subject: [PATCH 11/25] python: adding base pyproject.toml to python bindings project --- python/pyproject.toml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 python/pyproject.toml diff --git a/python/pyproject.toml b/python/pyproject.toml new file mode 100644 index 0000000..cadcd98 --- /dev/null +++ b/python/pyproject.toml @@ -0,0 +1,16 @@ +[build-system] +requires = ["hatchling >= 1.26"] +build-backend = "hatchling.build" + +[project] +name = "pool-day" +version = "0.1.1" +requires-python = ">=3.7" +authors = [{ name = "André L. C. Moreira", email = "andrelcmoreira@disroot.org" }] +description = "Python bindings to pool-day library" +license = "LGPL-3.0-only" +license-files = ["LICEN[CS]E.*"] +readme = { file = "README.md", content-type = "text/markdown" } + +[project.urls] +Repository = "https://github.com/andrelcmoreira/pool-day.git" From a9d75835abee8f07e0adeee8d7ea3494f6f554a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 16:27:13 -0300 Subject: [PATCH 12/25] python: fixing build --- python/pool_day/__init__.py | 2 +- python/pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 9afe4c1..e180d56 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -26,7 +26,7 @@ def _cb(param): return _cb -_pd_handle = cdll('../build/libpool-day.so') +_pd_handle = cdll('/usr/lib/libpool-day.so') # create_pool _pd_handle.create_pool.argtypes = [c_uint8] diff --git a/python/pyproject.toml b/python/pyproject.toml index cadcd98..2fe9db0 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -10,7 +10,7 @@ authors = [{ name = "André L. C. Moreira", email = "andrelcmoreira@disroot.org" description = "Python bindings to pool-day library" license = "LGPL-3.0-only" license-files = ["LICEN[CS]E.*"] -readme = { file = "README.md", content-type = "text/markdown" } +readme = { file = "../README.md", content-type = "text/markdown" } [project.urls] Repository = "https://github.com/andrelcmoreira/pool-day.git" From d9803a004a62142809015277dc4d7e2d3ab260e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 20:55:17 -0300 Subject: [PATCH 13/25] python: refactoring folder layout --- python/pool_day/__init__.py | 59 +++++-------------------------- python/pool_day/c_defs.py | 53 +++++++++++++++++++++++++++ python/{ => pool_day}/pool_day.py | 24 +++++++++---- 3 files changed, 80 insertions(+), 56 deletions(-) create mode 100644 python/pool_day/c_defs.py rename python/{ => pool_day}/pool_day.py (57%) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index e180d56..0de9ecb 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,53 +1,12 @@ -from ctypes import CDLL as cdll -from ctypes import POINTER as c_pointer -from ctypes import CFUNCTYPE as c_func_type -from ctypes import ( - Structure, - c_int, - c_uint8, - c_void_p, +from .pool_day import ( + create_pool, + enqueue_task, + create_task, + abort_tasks, + queued_tasks, ) +from pool_day.c_defs import pool_day_callback -class PoolDay(Structure): - pass - - -class Task(Structure): - pass - - -def pool_day_callback(cb): - - @c_func_type(c_void_p, c_void_p) - def _cb(param): - cb(param) - - return _cb - - -_pd_handle = cdll('/usr/lib/libpool-day.so') - -# create_pool -_pd_handle.create_pool.argtypes = [c_uint8] -_pd_handle.create_pool.restype = c_pointer(PoolDay) - -# destroy_pool -_pd_handle.destroy_pool.argtypes = [c_void_p] -_pd_handle.destroy_pool.restype = c_int - -# abort_tasks -_pd_handle.abort_tasks.argtypes = [c_pointer(PoolDay)] -_pd_handle.abort_tasks.restype = c_int - -# enqueue_task -_pd_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] -_pd_handle.enqueue_task.restype = c_int - -# queued_tasks -_pd_handle.queued_tasks.argtypes = [c_pointer(PoolDay)] -_pd_handle.queued_tasks.restype = c_uint8 - -# create_task -_pd_handle.create_task.argtypes = [c_void_p, c_void_p] -_pd_handle.create_task.restype = c_pointer(Task) +__all__ = ['create_pool', 'enqueue_task', 'create_task', + 'abort_tasks', 'queued_tasks', 'pool_day_callback'] diff --git a/python/pool_day/c_defs.py b/python/pool_day/c_defs.py new file mode 100644 index 0000000..e180d56 --- /dev/null +++ b/python/pool_day/c_defs.py @@ -0,0 +1,53 @@ +from ctypes import CDLL as cdll +from ctypes import POINTER as c_pointer +from ctypes import CFUNCTYPE as c_func_type +from ctypes import ( + Structure, + c_int, + c_uint8, + c_void_p, +) + + +class PoolDay(Structure): + pass + + +class Task(Structure): + pass + + +def pool_day_callback(cb): + + @c_func_type(c_void_p, c_void_p) + def _cb(param): + cb(param) + + return _cb + + +_pd_handle = cdll('/usr/lib/libpool-day.so') + +# create_pool +_pd_handle.create_pool.argtypes = [c_uint8] +_pd_handle.create_pool.restype = c_pointer(PoolDay) + +# destroy_pool +_pd_handle.destroy_pool.argtypes = [c_void_p] +_pd_handle.destroy_pool.restype = c_int + +# abort_tasks +_pd_handle.abort_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.abort_tasks.restype = c_int + +# enqueue_task +_pd_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] +_pd_handle.enqueue_task.restype = c_int + +# queued_tasks +_pd_handle.queued_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.queued_tasks.restype = c_uint8 + +# create_task +_pd_handle.create_task.argtypes = [c_void_p, c_void_p] +_pd_handle.create_task.restype = c_pointer(Task) diff --git a/python/pool_day.py b/python/pool_day/pool_day.py similarity index 57% rename from python/pool_day.py rename to python/pool_day/pool_day.py index 6d22fea..331e47c 100644 --- a/python/pool_day.py +++ b/python/pool_day/pool_day.py @@ -1,31 +1,43 @@ from contextlib import contextmanager from ctypes import byref -from pool_day import _pd_handle +from pool_day.c_defs import _pd_handle, PoolDay, Task @contextmanager -def create_pool(size): +def create_pool(size: int): + """ + """ pool = _pd_handle.create_pool(size) yield pool _pd_handle.destroy_pool(byref(pool)) -def destroy_pool(pool): +def destroy_pool(pool: PoolDay): + """ + """ return _pd_handle.destroy_pool(byref(pool)) -def enqueue_task(pool, task): +def enqueue_task(pool: PoolDay, task: Task): + """ + """ return _pd_handle.enqueue_task(pool, task) def create_task(cb, param): + """ + """ return _pd_handle.create_task(cb, param) -def abort_tasks(pool): +def abort_tasks(pool: PoolDay): + """ + """ return _pd_handle.abort_tasks(pool) -def queued_tasks(pool): +def queued_tasks(pool: PoolDay) -> int: + """ + """ return _pd_handle.queued_tasks(pool) From 5a9b212c478218a0dd95223c1a68a5ce667cea12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 21:00:18 -0300 Subject: [PATCH 14/25] ci: adding build_python_package job --- .github/workflows/ci.yaml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ea97eac..cebcd62 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -159,3 +159,22 @@ jobs: with: name: coverage-report path: ${{ env.COVERAGE_REPORT_DIR }} + + build_python_package: + name: Build Python package + runs-on: ubuntu-latest + needs: build + container: + image: andrelcmoreira/pool-day:v2 + volumes: + - ${{ github.workspace }}:/pool-day + credentials: + username: ${{ secrets.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_PASS }} + steps: + - uses: actions/checkout@v2 + + - name: Build package + run: | + cd python + pip install . From 763a4dbbb85b550beb13758f4f514548e782d229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 21:11:11 -0300 Subject: [PATCH 15/25] python: adding wait_task_finish function --- python/pool_day/__init__.py | 7 ++++--- python/pool_day/c_defs.py | 6 +++++- python/pool_day/pool_day.py | 6 ++++++ samples/python/sample.py | 9 +++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 0de9ecb..af90c2e 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,12 +1,13 @@ +from pool_day.c_defs import pool_day_callback from .pool_day import ( create_pool, enqueue_task, create_task, abort_tasks, queued_tasks, + wait_task_finish ) -from pool_day.c_defs import pool_day_callback -__all__ = ['create_pool', 'enqueue_task', 'create_task', - 'abort_tasks', 'queued_tasks', 'pool_day_callback'] +__all__ = ['create_pool', 'enqueue_task', 'create_task', 'abort_tasks', + 'queued_tasks', 'pool_day_callback', 'wait_task_finish'] diff --git a/python/pool_day/c_defs.py b/python/pool_day/c_defs.py index e180d56..3a36115 100644 --- a/python/pool_day/c_defs.py +++ b/python/pool_day/c_defs.py @@ -21,7 +21,7 @@ def pool_day_callback(cb): @c_func_type(c_void_p, c_void_p) def _cb(param): - cb(param) + cb(param) # TODO: return the task result properly return _cb @@ -51,3 +51,7 @@ def _cb(param): # create_task _pd_handle.create_task.argtypes = [c_void_p, c_void_p] _pd_handle.create_task.restype = c_pointer(Task) + +# wait_task_finish +_pd_handle.wait_task_finish.argtypes = [c_pointer(PoolDay), c_pointer(Task)] +_pd_handle.wait_task_finish.restype = c_void_p diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index 331e47c..543f38c 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -41,3 +41,9 @@ def queued_tasks(pool: PoolDay) -> int: """ """ return _pd_handle.queued_tasks(pool) + + +def wait_task_finish(pool: PoolDay, task: Task): + """ + """ + return _pd_handle.wait_task_finish(pool, task) diff --git a/samples/python/sample.py b/samples/python/sample.py index 37cbd9f..9023b14 100644 --- a/samples/python/sample.py +++ b/samples/python/sample.py @@ -5,7 +5,8 @@ create_task, enqueue_task, pool_day_callback, - queued_tasks + queued_tasks, + wait_task_finish ) @@ -15,6 +16,8 @@ def thread_cb(param): print('thread %d: hello from python callback, i: %d' % (param, i)) sleep(param) + return f'task {param} done' + def main(): with create_pool(2) as pool: @@ -26,7 +29,9 @@ def main(): print('enqueue_task ret =', enqueue_task(pool, t2)) print('enqueue_task ret =', enqueue_task(pool, t3)) print('queued_tasks ret =', queued_tasks(pool)) - input('') + + ret = wait_task_finish(pool, t3) + print(ret) if __name__ == "__main__": From d0961ccb1f5d7718b601fbc5e562b4a5168bffec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 21:56:35 -0300 Subject: [PATCH 16/25] python: encapsulating the binded methods into the PoolDay class --- python/pool_day/__init__.py | 12 ++------ python/pool_day/c_defs.py | 16 +++++----- python/pool_day/pool_day.py | 60 ++++++++++++++++++------------------- samples/python/sample.py | 27 +++++++---------- 4 files changed, 49 insertions(+), 66 deletions(-) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index af90c2e..1e208a9 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,13 +1,5 @@ from pool_day.c_defs import pool_day_callback -from .pool_day import ( - create_pool, - enqueue_task, - create_task, - abort_tasks, - queued_tasks, - wait_task_finish -) +from .pool_day import create_pool, PoolDay -__all__ = ['create_pool', 'enqueue_task', 'create_task', 'abort_tasks', - 'queued_tasks', 'pool_day_callback', 'wait_task_finish'] +__all__ = ['PoolDay', 'create_pool', 'pool_day_callback'] diff --git a/python/pool_day/c_defs.py b/python/pool_day/c_defs.py index 3a36115..4a18977 100644 --- a/python/pool_day/c_defs.py +++ b/python/pool_day/c_defs.py @@ -9,11 +9,11 @@ ) -class PoolDay(Structure): +class _PoolDay(Structure): pass -class Task(Structure): +class _Task(Structure): pass @@ -30,28 +30,28 @@ def _cb(param): # create_pool _pd_handle.create_pool.argtypes = [c_uint8] -_pd_handle.create_pool.restype = c_pointer(PoolDay) +_pd_handle.create_pool.restype = c_pointer(_PoolDay) # destroy_pool _pd_handle.destroy_pool.argtypes = [c_void_p] _pd_handle.destroy_pool.restype = c_int # abort_tasks -_pd_handle.abort_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.abort_tasks.argtypes = [c_pointer(_PoolDay)] _pd_handle.abort_tasks.restype = c_int # enqueue_task -_pd_handle.enqueue_task.argtypes = [c_pointer(PoolDay), c_pointer(Task)] +_pd_handle.enqueue_task.argtypes = [c_pointer(_PoolDay), c_pointer(_Task)] _pd_handle.enqueue_task.restype = c_int # queued_tasks -_pd_handle.queued_tasks.argtypes = [c_pointer(PoolDay)] +_pd_handle.queued_tasks.argtypes = [c_pointer(_PoolDay)] _pd_handle.queued_tasks.restype = c_uint8 # create_task _pd_handle.create_task.argtypes = [c_void_p, c_void_p] -_pd_handle.create_task.restype = c_pointer(Task) +_pd_handle.create_task.restype = c_pointer(_Task) # wait_task_finish -_pd_handle.wait_task_finish.argtypes = [c_pointer(PoolDay), c_pointer(Task)] +_pd_handle.wait_task_finish.argtypes = [c_pointer(_PoolDay), c_pointer(_Task)] _pd_handle.wait_task_finish.restype = c_void_p diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index 543f38c..f0c009f 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -1,49 +1,47 @@ from contextlib import contextmanager from ctypes import byref -from pool_day.c_defs import _pd_handle, PoolDay, Task +from pool_day.c_defs import _pd_handle, _Task, _PoolDay -@contextmanager -def create_pool(size: int): - """ - """ - pool = _pd_handle.create_pool(size) - yield pool - _pd_handle.destroy_pool(byref(pool)) +class PoolDay: - -def destroy_pool(pool: PoolDay): """ """ - return _pd_handle.destroy_pool(byref(pool)) + def __init__(self, pool: _PoolDay): + self._pool = pool -def enqueue_task(pool: PoolDay, task: Task): - """ - """ - return _pd_handle.enqueue_task(pool, task) + def enqueue_task(self, task: _Task): + """ + """ + return _pd_handle.enqueue_task(self._pool, task) + def create_task(self, cb, param) -> _Task: + """ + """ + return _pd_handle.create_task(cb, param) -def create_task(cb, param): - """ - """ - return _pd_handle.create_task(cb, param) + def abort_tasks(self): + """ + """ + return _pd_handle.abort_tasks(self._pool) + def queued_tasks(self) -> int: + """ + """ + return _pd_handle.queued_tasks(self._pool) -def abort_tasks(pool: PoolDay): - """ - """ - return _pd_handle.abort_tasks(pool) - - -def queued_tasks(pool: PoolDay) -> int: - """ - """ - return _pd_handle.queued_tasks(pool) + def wait_task_finish(self, task: _Task): + """ + """ + return _pd_handle.wait_task_finish(self._pool, task) -def wait_task_finish(pool: PoolDay, task: Task): +@contextmanager +def create_pool(size: int): """ """ - return _pd_handle.wait_task_finish(pool, task) + pool = _pd_handle.create_pool(size) + yield PoolDay(pool) + _pd_handle.destroy_pool(byref(pool)) diff --git a/samples/python/sample.py b/samples/python/sample.py index 9023b14..af3a7f1 100644 --- a/samples/python/sample.py +++ b/samples/python/sample.py @@ -1,19 +1,12 @@ from time import sleep -from pool_day import ( - create_pool, - create_task, - enqueue_task, - pool_day_callback, - queued_tasks, - wait_task_finish -) +from pool_day import create_pool, pool_day_callback @pool_day_callback def thread_cb(param): for i in range(1, 10): - print('thread %d: hello from python callback, i: %d' % (param, i)) + print(f'thread {param}: hello from python callback, i: {i}') sleep(param) return f'task {param} done' @@ -21,16 +14,16 @@ def thread_cb(param): def main(): with create_pool(2) as pool: - t1 = create_task(thread_cb, 1) - t2 = create_task(thread_cb, 2) - t3 = create_task(thread_cb, 3) + t1 = pool.create_task(thread_cb, 1) + t2 = pool.create_task(thread_cb, 2) + t3 = pool.create_task(thread_cb, 3) - print('enqueue_task ret =', enqueue_task(pool, t1)) - print('enqueue_task ret =', enqueue_task(pool, t2)) - print('enqueue_task ret =', enqueue_task(pool, t3)) - print('queued_tasks ret =', queued_tasks(pool)) + print('enqueue_task ret =', pool.enqueue_task(t1)) + print('enqueue_task ret =', pool.enqueue_task(t2)) + print('enqueue_task ret =', pool.enqueue_task(t3)) + print('queued_tasks ret =', pool.queued_tasks()) - ret = wait_task_finish(pool, t3) + ret = pool.wait_task_finish(t3) print(ret) From 2af2a2996b6d33994352a04397f619d86013260b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sat, 18 Oct 2025 23:26:43 -0300 Subject: [PATCH 17/25] ci: adding pylint job to CI workflow --- .github/workflows/ci.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index cebcd62..b5571fd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,6 +10,7 @@ env: COVERAGE_REPORT_DIR: build/coverage MINIMUM_COVERAGE: 85 LD_PRELOAD: LD_PRELOAD=build/test/src/libc/libpreload.so + PYLINT_MINIMUM_RATE: 9 jobs: build: @@ -178,3 +179,19 @@ jobs: run: | cd python pip install . + + lint: + name: Lint the Python bindings + runs-on: ubuntu-latest + container: + image: andrelcmoreira/pool-day:v2 + volumes: + - ${{ github.workspace }}:/pool-day + credentials: + username: ${{ secrets.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_PASS }} + steps: + - uses: actions/checkout@v2 + + - name: Run pylint + run: pylint python/pool_day --fail-under ${{ env.PYLINT_MINIMUM_RATE }} From 5cab14bd070e0950a38dd6d1cdff97d9315bbd88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sun, 19 Oct 2025 12:47:56 -0300 Subject: [PATCH 18/25] python: removing the method 'create_task' from PoolDay class --- python/pool_day/__init__.py | 4 ++-- python/pool_day/pool_day.py | 11 ++++++----- samples/python/sample.py | 8 ++++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index 1e208a9..de3c251 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,5 +1,5 @@ from pool_day.c_defs import pool_day_callback -from .pool_day import create_pool, PoolDay +from .pool_day import create_pool, create_task, PoolDay -__all__ = ['PoolDay', 'create_pool', 'pool_day_callback'] +__all__ = ['PoolDay', 'create_pool', 'create_task', 'pool_day_callback'] diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index f0c009f..fe255b4 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -17,11 +17,6 @@ def enqueue_task(self, task: _Task): """ return _pd_handle.enqueue_task(self._pool, task) - def create_task(self, cb, param) -> _Task: - """ - """ - return _pd_handle.create_task(cb, param) - def abort_tasks(self): """ """ @@ -38,6 +33,12 @@ def wait_task_finish(self, task: _Task): return _pd_handle.wait_task_finish(self._pool, task) +def create_task(cb, param) -> _Task: + """ + """ + return _pd_handle.create_task(cb, param) + + @contextmanager def create_pool(size: int): """ diff --git a/samples/python/sample.py b/samples/python/sample.py index af3a7f1..dd96cdc 100644 --- a/samples/python/sample.py +++ b/samples/python/sample.py @@ -1,6 +1,6 @@ from time import sleep -from pool_day import create_pool, pool_day_callback +from pool_day import create_pool, create_task, pool_day_callback @pool_day_callback @@ -14,9 +14,9 @@ def thread_cb(param): def main(): with create_pool(2) as pool: - t1 = pool.create_task(thread_cb, 1) - t2 = pool.create_task(thread_cb, 2) - t3 = pool.create_task(thread_cb, 3) + t1 = create_task(thread_cb, 1) + t2 = create_task(thread_cb, 2) + t3 = create_task(thread_cb, 3) print('enqueue_task ret =', pool.enqueue_task(t1)) print('enqueue_task ret =', pool.enqueue_task(t2)) From 4ef5f74a36496a2739133236d3d09dcf458ba83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sun, 19 Oct 2025 14:07:03 -0300 Subject: [PATCH 19/25] python: adding docstrings --- python/pool_day/pool_day.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index fe255b4..917009f 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -7,6 +7,9 @@ class PoolDay: """ + PoolDay instance. + + Implements a wrapper around the pool-day C library. """ def __init__(self, pool: _PoolDay): @@ -14,27 +17,47 @@ def __init__(self, pool: _PoolDay): def enqueue_task(self, task: _Task): """ + Enqueue a task to the pool. + + :task: The task instance. + :return: 0 on success, the suitable error code on failure. """ return _pd_handle.enqueue_task(self._pool, task) def abort_tasks(self): """ + Abort all pending tasks in the pool. + + :return: 0 on success, the suitable error code on failure. """ return _pd_handle.abort_tasks(self._pool) def queued_tasks(self) -> int: """ + Get the number of queued tasks in the pool. + + :return: Number of queued tasks. """ return _pd_handle.queued_tasks(self._pool) def wait_task_finish(self, task: _Task): """ + Wait for a task to finish. + + :task: The task instance. + :return: The return value of the task's callback function. """ return _pd_handle.wait_task_finish(self._pool, task) def create_task(cb, param) -> _Task: """ + Create a new pool's task instance. + + :cb: The callback function. + :param: The parameter passed to the callback function. + + :return: A new task instance. """ return _pd_handle.create_task(cb, param) @@ -42,6 +65,11 @@ def create_task(cb, param) -> _Task: @contextmanager def create_pool(size: int): """ + Create a new pool instance. + + :size: Number of threads in the pool. + + :return: A pool-day instance. """ pool = _pd_handle.create_pool(size) yield PoolDay(pool) From ee2cd141076a90d55fbb02d9fe90a285ce6a34a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sun, 19 Oct 2025 17:00:41 -0300 Subject: [PATCH 20/25] python: adding pylintrc file --- python/pylintrc | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 python/pylintrc diff --git a/python/pylintrc b/python/pylintrc new file mode 100644 index 0000000..0a644ec --- /dev/null +++ b/python/pylintrc @@ -0,0 +1,2 @@ +[MESSAGES CONTROL] +disable=too-few-public-methods From 3a1979d8447abf7f4d1ba33c1c22d94badf0bb10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Sun, 19 Oct 2025 17:09:48 -0300 Subject: [PATCH 21/25] ci: updating Dockerfile --- .github/workflows/ci.yaml | 27 +++++++++++++++------------ Dockerfile | 2 +- python/pool_day/pool_day.py | 9 +++++++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b5571fd..747fdab 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,7 +20,7 @@ jobs: matrix: cc: [gcc, clang] container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -28,6 +28,7 @@ jobs: password: ${{ secrets.DOCKERHUB_PASS }} steps: - uses: actions/checkout@v2 + - name: Build library run: | cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=${{ matrix.cc }} -S . -B build @@ -37,7 +38,7 @@ jobs: name: Build samples runs-on: ubuntu-latest container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -45,6 +46,7 @@ jobs: password: ${{ secrets.DOCKERHUB_PASS }} steps: - uses: actions/checkout@v2 + - name: Build samples run: | cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SAMPLES=ON -S . -B build @@ -55,7 +57,7 @@ jobs: runs-on: ubuntu-latest needs: build container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -93,7 +95,7 @@ jobs: runs-on: ubuntu-latest needs: unit_tests container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -114,7 +116,7 @@ jobs: name: Check documentation runs-on: ubuntu-latest container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -122,6 +124,7 @@ jobs: password: ${{ secrets.DOCKERHUB_PASS }} steps: - uses: actions/checkout@v2 + - name: Build library documentation run: | cmake -DBUILD_DOCUMENTATION=ON -S . -B build @@ -132,7 +135,7 @@ jobs: runs-on: ubuntu-latest needs: unit_tests container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -162,11 +165,11 @@ jobs: path: ${{ env.COVERAGE_REPORT_DIR }} build_python_package: - name: Build Python package + name: Build python package runs-on: ubuntu-latest needs: build container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -178,13 +181,13 @@ jobs: - name: Build package run: | cd python - pip install . + pip install . --break-system-packages lint: - name: Lint the Python bindings + name: Lint the python bindings runs-on: ubuntu-latest container: - image: andrelcmoreira/pool-day:v2 + image: andrelcmoreira/pool-day:v3 volumes: - ${{ github.workspace }}:/pool-day credentials: @@ -194,4 +197,4 @@ jobs: - uses: actions/checkout@v2 - name: Run pylint - run: pylint python/pool_day --fail-under ${{ env.PYLINT_MINIMUM_RATE }} + run: pylint --rcfile python/pylintrc python/pool_day --fail-under ${{ env.PYLINT_MINIMUM_RATE }} diff --git a/Dockerfile b/Dockerfile index 33648c2..6f3d426 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ RUN apt install cmake gcc g++ clang -y # building tools RUN apt install doxygen python3 python3-pip -y # utils RUN apt install cppcheck gcovr valgrind -y # qa tools RUN apt install libgmock-dev libgtest-dev -y # frameworks -RUN pip3 install --break-system-packages cpplint # python packages +RUN pip3 install --break-system-packages cpplint pylint # python packages WORKDIR /pool-day diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index 917009f..3474728 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -13,9 +13,14 @@ class PoolDay: """ def __init__(self, pool: _PoolDay): + """ + Initialize the PoolDay instance. + + :pool: The pool structure instance. + """ self._pool = pool - def enqueue_task(self, task: _Task): + def enqueue_task(self, task: _Task) -> int: """ Enqueue a task to the pool. @@ -24,7 +29,7 @@ def enqueue_task(self, task: _Task): """ return _pd_handle.enqueue_task(self._pool, task) - def abort_tasks(self): + def abort_tasks(self) -> int: """ Abort all pending tasks in the pool. From 2b6ed7aa734bd58cd907e23331dcb02aab68267f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Mon, 20 Oct 2025 20:51:21 -0300 Subject: [PATCH 22/25] python: adding BUILD_PYTHON_BINDINGS to CMakeLists file --- CMakeLists.txt | 5 +++++ README.md | 9 +++++---- python/CMakeLists.txt | 4 ++++ python/pool_day/c_defs.py | 22 ++++++++++++---------- python/pool_day/pool_day.py | 16 ++++++++++------ python/pyproject.toml | 2 ++ samples/python/sample.py | 8 ++++---- 7 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 python/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index fa10940..29fc9b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ option(BUILD_UNIT_TESTS "build the unit tests of the library" OFF) option(BUILD_DOCUMENTATION "build the documentation of the library" OFF) option(BUILD_COVERAGE "build the coverage data" OFF) option(ENABLE_LOGGING "enable library logs" OFF) +option(BUILD_PYTHON_BINDINGS "build the library's python bindings" OFF) set(PROJECT_NAME "pool-day") set(SOURCES src/task.c src/queue.c src/pool_day.c) @@ -45,6 +46,10 @@ if(ENABLE_LOGGING) list(APPEND SOURCES "src/logger.c") endif(ENABLE_LOGGING) +if(BUILD_PYTHON_BINDINGS) + add_subdirectory(python) +endif(BUILD_PYTHON_BINDINGS) + add_library(${PROJECT_NAME} SHARED ${SOURCES}) target_link_libraries(${PROJECT_NAME} pthread) diff --git a/README.md b/README.md index 98f0d0d..dabc7d1 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,11 @@ $ sudo cmake --install build Additional flags can be supplied as parameter to cmake according to the table below: -| Flag | Description | -|----------------|--------------------------------------| -| BUILD_SAMPLES | Build the library's samples | -| ENABLE_LOGGING | Enable the library's logging feature | +| Flag | Description | +|---------------------|----------------------------------------| +| BUILD_SAMPLES | Build the library's samples | +| ENABLE_LOGGING | Enable the library's logging feature | +|BUILD_PYTHON_BINDINGS| Build the library's python bindings | #### Support diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt new file mode 100644 index 0000000..c128320 --- /dev/null +++ b/python/CMakeLists.txt @@ -0,0 +1,4 @@ +#cmake_minimum_required(VERSION 3.18) +# +#find_package(Python3 COMPONENTS Interpreter Development REQUIRED) +# TODO diff --git a/python/pool_day/c_defs.py b/python/pool_day/c_defs.py index 4a18977..4b1094b 100644 --- a/python/pool_day/c_defs.py +++ b/python/pool_day/c_defs.py @@ -5,15 +5,15 @@ Structure, c_int, c_uint8, - c_void_p, + c_void_p ) -class _PoolDay(Structure): +class CPoolDay(Structure): pass -class _Task(Structure): +class CTask(Structure): pass @@ -21,7 +21,9 @@ def pool_day_callback(cb): @c_func_type(c_void_p, c_void_p) def _cb(param): - cb(param) # TODO: return the task result properly + ret = cb(param) + + return ret return _cb @@ -30,28 +32,28 @@ def _cb(param): # create_pool _pd_handle.create_pool.argtypes = [c_uint8] -_pd_handle.create_pool.restype = c_pointer(_PoolDay) +_pd_handle.create_pool.restype = c_pointer(CPoolDay) # destroy_pool _pd_handle.destroy_pool.argtypes = [c_void_p] _pd_handle.destroy_pool.restype = c_int # abort_tasks -_pd_handle.abort_tasks.argtypes = [c_pointer(_PoolDay)] +_pd_handle.abort_tasks.argtypes = [c_pointer(CPoolDay)] _pd_handle.abort_tasks.restype = c_int # enqueue_task -_pd_handle.enqueue_task.argtypes = [c_pointer(_PoolDay), c_pointer(_Task)] +_pd_handle.enqueue_task.argtypes = [c_pointer(CPoolDay), c_pointer(CTask)] _pd_handle.enqueue_task.restype = c_int # queued_tasks -_pd_handle.queued_tasks.argtypes = [c_pointer(_PoolDay)] +_pd_handle.queued_tasks.argtypes = [c_pointer(CPoolDay)] _pd_handle.queued_tasks.restype = c_uint8 # create_task _pd_handle.create_task.argtypes = [c_void_p, c_void_p] -_pd_handle.create_task.restype = c_pointer(_Task) +_pd_handle.create_task.restype = c_pointer(CTask) # wait_task_finish -_pd_handle.wait_task_finish.argtypes = [c_pointer(_PoolDay), c_pointer(_Task)] +_pd_handle.wait_task_finish.argtypes = [c_pointer(CPoolDay), c_pointer(CTask)] _pd_handle.wait_task_finish.restype = c_void_p diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index 3474728..b34515d 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -1,7 +1,11 @@ from contextlib import contextmanager from ctypes import byref -from pool_day.c_defs import _pd_handle, _Task, _PoolDay +from pool_day.c_defs import _pd_handle, CTask, CPoolDay + + +# TODO: task return +# TODO: when the first task finishes, the pool should be destroyed class PoolDay: @@ -12,7 +16,7 @@ class PoolDay: Implements a wrapper around the pool-day C library. """ - def __init__(self, pool: _PoolDay): + def __init__(self, pool: CPoolDay): """ Initialize the PoolDay instance. @@ -20,7 +24,7 @@ def __init__(self, pool: _PoolDay): """ self._pool = pool - def enqueue_task(self, task: _Task) -> int: + def enqueue_task(self, task: CTask) -> int: """ Enqueue a task to the pool. @@ -45,7 +49,7 @@ def queued_tasks(self) -> int: """ return _pd_handle.queued_tasks(self._pool) - def wait_task_finish(self, task: _Task): + def wait_task_finish(self, task: CTask): """ Wait for a task to finish. @@ -55,9 +59,9 @@ def wait_task_finish(self, task: _Task): return _pd_handle.wait_task_finish(self._pool, task) -def create_task(cb, param) -> _Task: +def create_task(cb, param) -> CTask: """ - Create a new pool's task instance. + Create a new task instance. :cb: The callback function. :param: The parameter passed to the callback function. diff --git a/python/pyproject.toml b/python/pyproject.toml index 2fe9db0..025cf2f 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -14,3 +14,5 @@ readme = { file = "../README.md", content-type = "text/markdown" } [project.urls] Repository = "https://github.com/andrelcmoreira/pool-day.git" + +# TODO: check dependencies for libpoolday.so diff --git a/samples/python/sample.py b/samples/python/sample.py index dd96cdc..084f183 100644 --- a/samples/python/sample.py +++ b/samples/python/sample.py @@ -18,10 +18,10 @@ def main(): t2 = create_task(thread_cb, 2) t3 = create_task(thread_cb, 3) - print('enqueue_task ret =', pool.enqueue_task(t1)) - print('enqueue_task ret =', pool.enqueue_task(t2)) - print('enqueue_task ret =', pool.enqueue_task(t3)) - print('queued_tasks ret =', pool.queued_tasks()) + print('task 1 enqueued, ret =', pool.enqueue_task(t1)) + print('task 2 enqueued, ret =', pool.enqueue_task(t2)) + print('task 3 enqueued, ret =', pool.enqueue_task(t3)) + print('number of queued tasks =', pool.queued_tasks()) ret = pool.wait_task_finish(t3) print(ret) From afd8c2e03dfdd22b1d9bb033980bc4175b63c2d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Fri, 24 Oct 2025 22:10:04 -0300 Subject: [PATCH 23/25] python: wrapping the task result into a TaskResult instance --- python/pool_day/__init__.py | 5 +++-- python/pool_day/c_defs.py | 21 ++++++++++++++++++++- python/pool_day/pool_day.py | 11 +++++++---- samples/python/sample.py | 6 ++++-- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/python/pool_day/__init__.py b/python/pool_day/__init__.py index de3c251..36ba6c3 100644 --- a/python/pool_day/__init__.py +++ b/python/pool_day/__init__.py @@ -1,5 +1,6 @@ from pool_day.c_defs import pool_day_callback -from .pool_day import create_pool, create_task, PoolDay +from .pool_day import create_pool, create_task, PoolDay, TaskResult -__all__ = ['PoolDay', 'create_pool', 'create_task', 'pool_day_callback'] +__all__ = ['PoolDay', 'TaskResult', 'create_pool', 'create_task', + 'pool_day_callback'] diff --git a/python/pool_day/c_defs.py b/python/pool_day/c_defs.py index 4b1094b..e4ad301 100644 --- a/python/pool_day/c_defs.py +++ b/python/pool_day/c_defs.py @@ -1,12 +1,17 @@ from ctypes import CDLL as cdll from ctypes import POINTER as c_pointer from ctypes import CFUNCTYPE as c_func_type +from ctypes import cast +from ctypes import byref from ctypes import ( Structure, c_int, c_uint8, - c_void_p + c_void_p, + c_int ) +from dataclasses import dataclass +from typing import Any class CPoolDay(Structure): @@ -17,17 +22,31 @@ class CTask(Structure): pass +@dataclass +class TaskResult: + result: Any + + def pool_day_callback(cb): @c_func_type(c_void_p, c_void_p) def _cb(param): ret = cb(param) + print('pool_day_callback ret:', ret) + print('pool_day_callback ret type:', type(ret)) + print('pool_day_callback param value:', param) + print('pool_day_callback param type:', type(param)) + return ret return _cb +def to_result(ptr: c_void_p) -> TaskResult: + return TaskResult(result=ptr) + + _pd_handle = cdll('/usr/lib/libpool-day.so') # create_pool diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index b34515d..2cdf8cf 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -1,11 +1,10 @@ from contextlib import contextmanager from ctypes import byref -from pool_day.c_defs import _pd_handle, CTask, CPoolDay +from pool_day.c_defs import _pd_handle, CTask, CPoolDay, TaskResult, to_result # TODO: task return -# TODO: when the first task finishes, the pool should be destroyed class PoolDay: @@ -49,14 +48,18 @@ def queued_tasks(self) -> int: """ return _pd_handle.queued_tasks(self._pool) - def wait_task_finish(self, task: CTask): + def wait_task_finish(self, task: CTask) -> TaskResult: """ Wait for a task to finish. :task: The task instance. :return: The return value of the task's callback function. """ - return _pd_handle.wait_task_finish(self._pool, task) + ret = _pd_handle.wait_task_finish(self._pool, task) + + print(f'wait_task_finish ret: {ret}') + + return to_result(ret) def create_task(cb, param) -> CTask: diff --git a/samples/python/sample.py b/samples/python/sample.py index 084f183..2a0e252 100644 --- a/samples/python/sample.py +++ b/samples/python/sample.py @@ -9,7 +9,8 @@ def thread_cb(param): print(f'thread {param}: hello from python callback, i: {i}') sleep(param) - return f'task {param} done' + #return f'task {param} done' + return param def main(): @@ -24,7 +25,8 @@ def main(): print('number of queued tasks =', pool.queued_tasks()) ret = pool.wait_task_finish(t3) - print(ret) + print(type(ret)) + #print(ret) if __name__ == "__main__": From f753d559d4386901b20e53acda261bd7663dd3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Fri, 14 Nov 2025 21:01:57 -0300 Subject: [PATCH 24/25] ci: splitting ci.yaml workflow into 'library.yaml' and 'python.yaml' --- .github/workflows/{ci.yaml => library.yaml} | 42 ++----------------- .github/workflows/python.yaml | 45 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 39 deletions(-) rename .github/workflows/{ci.yaml => library.yaml} (81%) create mode 100644 .github/workflows/python.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/library.yaml similarity index 81% rename from .github/workflows/ci.yaml rename to .github/workflows/library.yaml index 747fdab..182873b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/library.yaml @@ -1,4 +1,4 @@ -name: CI +name: Library on: pull_request: @@ -10,11 +10,10 @@ env: COVERAGE_REPORT_DIR: build/coverage MINIMUM_COVERAGE: 85 LD_PRELOAD: LD_PRELOAD=build/test/src/libc/libpreload.so - PYLINT_MINIMUM_RATE: 9 jobs: build: - name: Build library + name: Build runs-on: ubuntu-latest strategy: matrix: @@ -29,7 +28,7 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Build library + - name: Build run: | cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=${{ matrix.cc }} -S . -B build cmake --build build @@ -163,38 +162,3 @@ jobs: with: name: coverage-report path: ${{ env.COVERAGE_REPORT_DIR }} - - build_python_package: - name: Build python package - runs-on: ubuntu-latest - needs: build - container: - image: andrelcmoreira/pool-day:v3 - volumes: - - ${{ github.workspace }}:/pool-day - credentials: - username: ${{ secrets.DOCKERHUB_USER }} - password: ${{ secrets.DOCKERHUB_PASS }} - steps: - - uses: actions/checkout@v2 - - - name: Build package - run: | - cd python - pip install . --break-system-packages - - lint: - name: Lint the python bindings - runs-on: ubuntu-latest - container: - image: andrelcmoreira/pool-day:v3 - volumes: - - ${{ github.workspace }}:/pool-day - credentials: - username: ${{ secrets.DOCKERHUB_USER }} - password: ${{ secrets.DOCKERHUB_PASS }} - steps: - - uses: actions/checkout@v2 - - - name: Run pylint - run: pylint --rcfile python/pylintrc python/pool_day --fail-under ${{ env.PYLINT_MINIMUM_RATE }} diff --git a/.github/workflows/python.yaml b/.github/workflows/python.yaml new file mode 100644 index 0000000..7e97bbb --- /dev/null +++ b/.github/workflows/python.yaml @@ -0,0 +1,45 @@ +name: Python bindings + +on: + pull_request: + push: + branches: + - develop + +env: + PYLINT_MINIMUM_RATE: 9 + +jobs: + build: + name: Build python package + runs-on: ubuntu-latest + container: + image: andrelcmoreira/pool-day:v3 + volumes: + - ${{ github.workspace }}:/pool-day + credentials: + username: ${{ secrets.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_PASS }} + steps: + - uses: actions/checkout@v2 + + - name: Build package + run: | + cd python + pip install . --break-system-packages + + lint: + name: Lint the python bindings + runs-on: ubuntu-latest + container: + image: andrelcmoreira/pool-day:v3 + volumes: + - ${{ github.workspace }}:/pool-day + credentials: + username: ${{ secrets.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_PASS }} + steps: + - uses: actions/checkout@v2 + + - name: Run pylint + run: pylint --rcfile python/pylintrc python/pool_day --fail-under ${{ env.PYLINT_MINIMUM_RATE }} From a33c0dc30efe48853e79fa8e405fdb8d1ee381ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=2E=20C=2E=20Moreira?= Date: Mon, 13 Apr 2026 23:39:39 -0300 Subject: [PATCH 25/25] python: adapting the sample to the new API --- python/pool_day/c_defs.py | 24 +++++++++++++------- python/pool_day/pool_day.py | 23 ++++++++++++++----- python/pyproject.toml | 2 +- samples/python/{sample.py => sync-sample.py} | 13 ++++++----- 4 files changed, 41 insertions(+), 21 deletions(-) rename samples/python/{sample.py => sync-sample.py} (69%) diff --git a/python/pool_day/c_defs.py b/python/pool_day/c_defs.py index e4ad301..50930e9 100644 --- a/python/pool_day/c_defs.py +++ b/python/pool_day/c_defs.py @@ -6,9 +6,10 @@ from ctypes import ( Structure, c_int, - c_uint8, + c_uint32, c_void_p, - c_int + c_int, + c_size_t, ) from dataclasses import dataclass from typing import Any @@ -50,7 +51,7 @@ def to_result(ptr: c_void_p) -> TaskResult: _pd_handle = cdll('/usr/lib/libpool-day.so') # create_pool -_pd_handle.create_pool.argtypes = [c_uint8] +_pd_handle.create_pool.argtypes = [c_uint32] _pd_handle.create_pool.restype = c_pointer(CPoolDay) # destroy_pool @@ -67,12 +68,19 @@ def to_result(ptr: c_void_p) -> TaskResult: # queued_tasks _pd_handle.queued_tasks.argtypes = [c_pointer(CPoolDay)] -_pd_handle.queued_tasks.restype = c_uint8 +_pd_handle.queued_tasks.restype = c_uint32 + +# create_sync_task +_pd_handle.create_sync_task.argtypes = [c_uint32, c_void_p, c_void_p, c_size_t] +_pd_handle.create_sync_task.restype = c_pointer(CTask) -# create_task -_pd_handle.create_task.argtypes = [c_void_p, c_void_p] -_pd_handle.create_task.restype = c_pointer(CTask) +# create_async_task +_pd_handle.create_async_task.argtypes = [c_uint32, c_void_p, c_void_p, c_size_t, c_void_p, c_void_p] +_pd_handle.create_async_task.restype = c_pointer(CTask) # wait_task_finish -_pd_handle.wait_task_finish.argtypes = [c_pointer(CPoolDay), c_pointer(CTask)] +_pd_handle.wait_task_finish.argtypes = [c_pointer(CTask)] _pd_handle.wait_task_finish.restype = c_void_p + +# get_task_result +_pd_handle.get_task_result.argtypes = [c_pointer(CTask)] diff --git a/python/pool_day/pool_day.py b/python/pool_day/pool_day.py index 2cdf8cf..ce2fb49 100644 --- a/python/pool_day/pool_day.py +++ b/python/pool_day/pool_day.py @@ -48,30 +48,41 @@ def queued_tasks(self) -> int: """ return _pd_handle.queued_tasks(self._pool) - def wait_task_finish(self, task: CTask) -> TaskResult: + def wait_task_finish(self, task: CTask) -> None: """ Wait for a task to finish. + :task: The task instance. + """ + _pd_handle.wait_task_finish(self._pool, task) + + def get_task_result(self, task: CTask) -> TaskResult: + """ + Get a task result. + :task: The task instance. :return: The return value of the task's callback function. """ - ret = _pd_handle.wait_task_finish(self._pool, task) + ret = _pd_handle.get_task_result(self._pool, task) - print(f'wait_task_finish ret: {ret}') + print(f'get_task_result ret: {ret}') return to_result(ret) -def create_task(cb, param) -> CTask: +def create_sync_task(cb, param) -> CTask: """ - Create a new task instance. + Create a new sync task instance. :cb: The callback function. :param: The parameter passed to the callback function. :return: A new task instance. """ - return _pd_handle.create_task(cb, param) + return _pd_handle.create_sync_task(cb, param) + + +# TODO: create_async_task @contextmanager diff --git a/python/pyproject.toml b/python/pyproject.toml index 025cf2f..9ae22c1 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" name = "pool-day" version = "0.1.1" requires-python = ">=3.7" -authors = [{ name = "André L. C. Moreira", email = "andrelcmoreira@disroot.org" }] +authors = [{ name = "André L. C. Moreira", email = "andrelcmoreira@proton.me" }] description = "Python bindings to pool-day library" license = "LGPL-3.0-only" license-files = ["LICEN[CS]E.*"] diff --git a/samples/python/sample.py b/samples/python/sync-sample.py similarity index 69% rename from samples/python/sample.py rename to samples/python/sync-sample.py index 2a0e252..ff57a74 100644 --- a/samples/python/sample.py +++ b/samples/python/sync-sample.py @@ -1,6 +1,6 @@ from time import sleep -from pool_day import create_pool, create_task, pool_day_callback +from pool_day import create_pool, create_sync_task, pool_day_callback @pool_day_callback @@ -15,18 +15,19 @@ def thread_cb(param): def main(): with create_pool(2) as pool: - t1 = create_task(thread_cb, 1) - t2 = create_task(thread_cb, 2) - t3 = create_task(thread_cb, 3) + t1 = create_sync_task(thread_cb, 1) + t2 = create_sync_task(thread_cb, 2) + t3 = create_sync_task(thread_cb, 3) print('task 1 enqueued, ret =', pool.enqueue_task(t1)) print('task 2 enqueued, ret =', pool.enqueue_task(t2)) print('task 3 enqueued, ret =', pool.enqueue_task(t3)) print('number of queued tasks =', pool.queued_tasks()) - ret = pool.wait_task_finish(t3) + ret = pool.get_task_result(t3) + print(type(ret)) - #print(ret) + print(ret) if __name__ == "__main__":