Developing on Golem #2 — Running our own commands
In this guide, you will learn how to run your own commands on a provider. For now, these commands will only be bash commands, but you will learn how to run Python Code with custom modules in the coming guides.
Disclaimer: This guide is the second part of a 6-part series. You should have gone through the first part before attempting anything here.
1 - Copying and running the example
In this chapter, we will be copying the code from the Hello World Task Example.
Then, we will run the example.
We should have our dependencies installed since the first guide already. This is not the case if you change environment, meaning that you may need to run “pip install -U pip” and “pip install yapapi” again.
Downloading the example
- Visit this link where the raw script is stored.
- Right click and save the Python script and remember where you place it.
(make sure that no text is selected, and if you have troubles you can search on how to make a Python file and copy the contents to one)
Navigating to our folder
- Find the folder where you downloaded the script to:
- Click the top and copy the navigation path:
- Open up a command prompt and run
cd
followed by your navigation path:
Running the example
- In your command prompt, enter
python hello.py
. - It should output something like this:
2 - Modifying our example
In this chapter, we will go through a few of the parts that you can change, and what they do.
At the end, you will know how to run your own shell commands on the provider.
Explaining what does what in the script
The highlighted code is simply put what runs on every provider and what is being done with that computation.
The important thing for us is script.run(), which makes us able to run commands on the provider.
Later on, we’ll be able to do things such as uploading and downloading files from here, and if you can get advanced enough you might be able to verify your results yourself here.
async def worker(context: WorkContext, tasks: AsyncIterable[Task]):
async for task in tasks:
script = context.new_script()
future_result = script.run("/bin/sh", "-c", "date")
yield script
task.accept_result(result=await future_result)
We will not touch this in this guide, but if we change the image_hash the provider will know, for example, what files and programs to install to be able to run our scripts.
package = await vm.repo(
image_hash="d646d7b93....",
)
Lastly, we can print out the output of the providers’ terminal from the previously set result = await future_result:
async with Golem(budget=1.0, subnet_tag="devnet-beta") as golem:
async for completed in golem.execute_tasks(worker, tasks, payload=package):
print(completed.result.stdout)
Modifying the script to run another command
If we go back to the part where we saw what is ran on the providers’ machine and handled locally, we can try changing just the command from date
to something else.
With that, change this highlighted part from here
async def worker(context: WorkContext, tasks: AsyncIterable[Task]):
async for task in tasks:
script = context.new_script()
future_result = script.run("/bin/sh", "-c", "date")
yield script
task.accept_result(result=await future_result)
To the highlighted part here
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"')
yield script
task.accept_result(result=await future_result)
This should now print out test in our console. Let’s try it out!
Running our modified example
- In your command prompt, enter
python hello.py
. - It should output something like this:
What’s next?
Now we have run our own shell command on a provider, but we don’t know how to run our own applications yet. In the following guides you will be:
- Running your own Python Code on a provider
- Configuring your own Dockerfiles to include Modules & Dependencies
- Going through a detailed example of Python Code, Modules, Dependencies, and Golem
- Exploring & Understanding Golem’s limitations, workarounds, and future development plans