Skip to content

Commit

Permalink
feat(fn): run every function in an event loop
Browse files Browse the repository at this point in the history
And then await it only if the value it returns is awaitable...
  • Loading branch information
wiwichips committed Jul 2, 2024
1 parent 5d0d683 commit 5e018f7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
14 changes: 9 additions & 5 deletions dcp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# NOTE TO SELF - have to load class registry before classes that use it

from .dry import make_dcp_class, class_registry, wrap_js_obj, aio_run_wrapper, blocking_run_wrapper
from .js import dcp_client as dcp_js
from .sanity import sanity
import sys
from types import ModuleType as Module

import pythonmonkey as pm
PMDict = pm.eval('x={};x').__class__
proto_own_prop_names = pm.eval('x=>(x?.prototype ? Object.getOwnPropertyNames(x?.prototype) : [])')
proto_own_prop_names = pm.eval(
'x=>(x?.prototype ? Object.getOwnPropertyNames(x?.prototype) : [])')

from .sanity import sanity
from .js import dcp_client as dcp_js
from .dry import make_dcp_class, class_registry, wrap_js_obj, aio_run_wrapper, blocking_run_wrapper

# state
init_memo = None
Expand Down Expand Up @@ -50,7 +51,10 @@ def init_dcp_module(py_parent, js_module, js_name):
if isinstance(prop_ref, pm.JSFunctionProxy):
# TODO: come up with better way to determine if class...
if len(proto_own_prop_names(prop_ref)) > 1:
setattr(module, prop_name, make_dcp_class(prop_ref, name=prop_name))
new_bfclass = make_dcp_class(prop_ref, name=prop_name)
class_registry.register(new_bfclass)
setattr(module, prop_name, new_bfclass)

# TODO: check if the function is known to return a promise...
else:
setattr(module, prop_name, blocking_run_wrapper(prop_ref))
Expand Down
8 changes: 3 additions & 5 deletions dcp/dry/bfclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# @date June 2024

import asyncio
import inspect
import types
import pythonmonkey as pm

Expand Down Expand Up @@ -43,7 +44,7 @@ def make_dcp_class(js_class, **kwargs):
'name': js_class_name(js_class),
'props': {},
'aio_methods': [],
'aio_ctor': True,
'aio_ctor': True, # TODO maybe shouldn't be true
'force_blocking': [],
'js_instance': None,
}
Expand All @@ -70,10 +71,7 @@ def __getattr__(self, name):
return js_attr

def method(*args, **kwargs):
if (name in opts.aio_methods or name in opts.force_blocking):
aio_attr = getattr(self.aio, name)
return asyncio.run(aio_attr(*args, **kwargs))
return js_attr(*args, **kwargs)
return asyncio.run(aio_run_wrapper(js_attr)(*args, **kwargs))

return method

Expand Down
3 changes: 2 additions & 1 deletion dcp/dry/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ async def aio_fn(*args, **kwargs):
if inspect.isawaitable(return_value):
return await return_value

print("AIO_RUN_WRAPPER_CASE_HIT_WHERE_FN_NOT_RETURN_CORO") # TODO???
# TODO: check class registry if it should be wrapped instance

return return_value
return aio_fn

Expand Down
25 changes: 25 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import pythonmonkey as pm
from dcp import job
import dcp
dcp.init()


my_j = job.Job('x=>{progress();return x+x;}', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

my_j.on('readystatechange', print)
my_j.on('accepted', lambda: print(my_j.id))
my_j.on('result', print)

my_j.computeGroups = [{'joinKey': 'joseph', 'joinSecret': 'pringle'}]
my_j.public.name = 'simple bifrost2 example'

res = my_j.exec()

# my_j.exec()
# res = my_j.wait()

print(">>>>>>>>>>>>>>>>>>>>>>>>>> RESULTS ARE IN")
print(pm.eval('Array.from')(res))

5 changes: 2 additions & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ def test_init(self):

self.assertEqual(ret_module, dcp)

job = job.Job('x=>{progress();return x+1', [1, 2, 3])
job = job.Job('x=>{progress();return x+1', [])
job.on('readystatechange', print)

# TODO - maybe I should just smoke test if a few functions exist?
# How do I test this?

def test_init_twice(self):
dcp.init()
Expand Down

0 comments on commit 5e018f7

Please sign in to comment.