Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsyncWorker class for asynchronous execution of functions #108

Merged
merged 12 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
codecov:
token: 89d5d00f-39e7-4e79-9d63-165b5efee4cb
cli:
plugins:
pycoverage:
report_type: "json"
token: 89d5d00f-39e7-4e79-9d63-165b5efee4cb
ignore:
- "frontend"
3 changes: 3 additions & 0 deletions neetbox/config/_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
parsed_local_config = _build_global_config_dict_for_module(
inspect.getmodule(fun), local_config
)
update_dict_recursively(_DEFAULT_WORKSPACE_CONFIG, parsed_local_config)

Check warning on line 118 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L118

Added line #L118 was not covered by tests
except Exception as e:
raise e

Expand All @@ -127,40 +127,41 @@


def _init_workspace(path=None, **kwargs) -> bool:
if path:
os.chdir(path=path)
current_path = os.getcwd()
config_file_path = os.path.join(current_path, CONFIG_FILE_NAME)
if not os.path.exists(config_file_path): # config file not exist
try: # creating config file using default config
with open(config_file_path, "w+") as config_file:
import neetbox.extension as extension

Check warning on line 137 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L130-L137

Added lines #L130 - L137 were not covered by tests

extension._scan_sub_modules()
_update_default_config_from_config_register() # load custom config into default config
_config = _DEFAULT_WORKSPACE_CONFIG
if "name" in kwargs and kwargs["name"]: # using given name
_config["name"] = kwargs["name"]

Check warning on line 143 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L139-L143

Added lines #L139 - L143 were not covered by tests
else: # using the folder name
_config["name"] = os.path.basename(os.path.normpath(os.getcwd()))
_config["projectid"] = str(uuid.uuid4())
toml.dump(_config, config_file)
return True
except Exception as e:
raise e

Check warning on line 150 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L145-L150

Added lines #L145 - L150 were not covered by tests
else: # config file exist:
raise RuntimeError(f"{config_file_path} already exist")

Check warning on line 152 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L152

Added line #L152 was not covered by tests


def _check_if_workspace_config_valid(path=None) -> bool:
path = path or "."
config_file_path = os.path.join(path, CONFIG_FILE_NAME)
if not os.path.isfile(config_file_path): # but config file not exist
return False

Check warning on line 159 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L159

Added line #L159 was not covered by tests
try:
toml.load(config_file_path) # try load as toml
return config_file_path
except Exception as e:
return False

Check warning on line 164 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L163-L164

Added lines #L163 - L164 were not covered by tests


def _load_workspace_config(folder="."):
Expand All @@ -168,17 +169,19 @@
path=folder
) # check if config file is valid
if not config_file_path: # failed to load workspace config, exiting
raise RuntimeError(f"Config file not exists in '{folder}'")

Check warning on line 172 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L172

Added line #L172 was not covered by tests
_update_default_config_from_config_register() # load custom config into default config
_obtain_new_run_id() # obtain new run id
_update_default_workspace_config_with(toml.load(config_file_path)) # load config file in


def _create_load_workspace(path=None):
is_workspace = _check_if_workspace_config_valid(path)
if not is_workspace:
_init_workspace(path)

Check warning on line 181 in neetbox/config/_workspace.py

View check run for this annotation

Codecov / codecov/patch

neetbox/config/_workspace.py#L181

Added line #L181 was not covered by tests
_load_workspace_config()
import neetbox.extension as extension

extension._scan_sub_modules()


Expand Down
30 changes: 30 additions & 0 deletions neetbox/utils/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import time
from concurrent.futures import ThreadPoolExecutor
from typing import Callable

Check warning on line 3 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L1-L3

Added lines #L1 - L3 were not covered by tests

MAX_WORKERS = 4

Check warning on line 5 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L5

Added line #L5 was not covered by tests


class AsyncWoker:
EXECUTOR = ThreadPoolExecutor(max_workers=MAX_WORKERS)

Check warning on line 9 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L8-L9

Added lines #L8 - L9 were not covered by tests

def __init__(self, func: Callable):
self.func = func

Check warning on line 12 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L11-L12

Added lines #L11 - L12 were not covered by tests

def __call__(self, *args, **kwargs):
return AsyncWoker.EXECUTOR.submit(self.func, *args, **kwargs) # type: ignore

Check warning on line 15 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L14-L15

Added lines #L14 - L15 were not covered by tests


if __name__ == "__main__":

Check warning on line 18 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L18

Added line #L18 was not covered by tests

@AsyncWoker
def add_image(image, a=1, b=2):
time.sleep(image)
print("b")
print(a, b)
return 5

Check warning on line 25 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L20-L25

Added lines #L20 - L25 were not covered by tests

for i in range(10):
print(f"i: {i}")
add_image(i)
add_image(i, a=2, b=2)

Check warning on line 30 in neetbox/utils/worker.py

View check run for this annotation

Codecov / codecov/patch

neetbox/utils/worker.py#L27-L30

Added lines #L27 - L30 were not covered by tests
Loading