From 01984297c54894e65ea355c69647eb9f9cd6d18e Mon Sep 17 00:00:00 2001 From: approxit Date: Tue, 31 Jan 2023 18:34:30 +0100 Subject: [PATCH 1/2] initial example --- .../environment_variables.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/environment-variables/environment_variables.py diff --git a/examples/environment-variables/environment_variables.py b/examples/environment-variables/environment_variables.py new file mode 100644 index 000000000..4aea270a5 --- /dev/null +++ b/examples/environment-variables/environment_variables.py @@ -0,0 +1,36 @@ +import asyncio +from typing import AsyncIterable + +from yapapi import Golem, Task, WorkContext +from yapapi.payload import vm + + +async def worker(context: WorkContext, tasks: AsyncIterable[Task]): + async for task in tasks: + script = context.new_script() + future_result = script.run( + "/bin/sh", "-c", "echo ${TEST_VAR:=not captured}", + env={ + "TEST_VAR": "captured", + } + ) + + yield script + + task.accept_result(result=await future_result) + + +async def main(): + package = await vm.repo( + image_hash="d646d7b93083d817846c2ae5c62c72ca0507782385a2e29291a3d376", + ) + + tasks = [Task(data=None)] + + async with Golem(budget=1.0, subnet_tag="public") as golem: + async for completed in golem.execute_tasks(worker, tasks, payload=package): + print('Environ variable in current run was:', completed.result.stdout) + + +if __name__ == "__main__": + asyncio.run(main()) From 687ed338c6dc81cda28dd7811727878c98c27e01 Mon Sep 17 00:00:00 2001 From: approxit Date: Wed, 1 Feb 2023 11:56:52 +0100 Subject: [PATCH 2/2] fix for sending env to yagna --- examples/environment-variables/environment_variables.py | 7 ++++++- yapapi/script/command.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/environment-variables/environment_variables.py b/examples/environment-variables/environment_variables.py index 4aea270a5..a2018c430 100644 --- a/examples/environment-variables/environment_variables.py +++ b/examples/environment-variables/environment_variables.py @@ -2,6 +2,7 @@ from typing import AsyncIterable from yapapi import Golem, Task, WorkContext +from yapapi.log import enable_default_logger from yapapi.payload import vm @@ -33,4 +34,8 @@ async def main(): if __name__ == "__main__": - asyncio.run(main()) + enable_default_logger(log_file="hello.log") + + loop = asyncio.get_event_loop() + task = loop.create_task(main()) + loop.run_until_complete(task) diff --git a/yapapi/script/command.py b/yapapi/script/command.py index 3d8ae94aa..60f897b04 100644 --- a/yapapi/script/command.py +++ b/yapapi/script/command.py @@ -213,11 +213,11 @@ def __init__( def evaluate(self): capture = {"stdout": self.stdout.to_dict(), "stderr": self.stderr.to_dict()} return self._make_batch_command( - "run", entry_point=self.cmd, args=self.args, capture=capture + "run", entry_point=self.cmd, args=self.args, env=self.env, capture=capture ) def __repr__(self): - return f"{super().__repr__()} {self.cmd} {self.args}" + return f"{super().__repr__()} {self.cmd} {self.args} {self.env}" StorageEvent = Union[DownloadStarted, DownloadFinished]