Developing on Golem #3 — Running your own Python Code on a provider

figurestudios
3 min readMar 11, 2022

--

In this guide, you will learn how to run your own Python commands on a provider. For now, you will not be able to use custom modules, but you will learn how to set up Dockerfiles with custom modules later on.

Disclaimer: This guide is the third part of a 6-part series. You should have gone through the first and second parts before attempting anything here.

1 - Modifying our script

In this chapter, we will change a few things in our script to make us able to run Python code.

We will change image hash and add some snippets that allows us to upload and run code on the provider.

You will not have to build your own image hash yet, as there will be one provided here.

  • If you don’t have your script open since the last time, this is the time to open it. It should be named hello.py . If you have no program installed to edit it, I recommend VSCode, where you can right click the file and open file.

Changing the image hash

  • Find the part that looks like this:
package = await vm.repo(
image_hash="d646d7b93083d817846c2ae5c62c72ca0507782385a2e29291a3d376",
)
  • Change the highlighted text to: eb26fc55959db9901148fc6dfaed8578b2f8fb7067c88feaeb1846f8

Changing the image hash makes the provider know what their environment should look like before running the task.

This one (eb26fc…) has Python installed, and you’ll learn how to make your own image hashes with other pre-installed modules and applications in the coming guides.

Uploading our (not yet made) Python script

  • Find the part that looks like this:
script = context.new_script()
future_result = script.run("/bin/sh", "-c", 'echo "test"')
yield script
task.accept_result(result=await future_result)
  • Add the highlighted snippet to make us able to upload a Python script:
script = context.new_script()
script.upload_file("provider.py", "/golem/input/provider.py")
future_result = script.run("/bin/sh", "-c", 'echo "test"')
yield script
task.accept_result(result=await future_result)

Running our (not yet made) Python script

  • In the same part, change this highlighted line:
script = context.new_script()
script.upload_file("provider.py", "/golem/input/provider.py")
future_result = script.run("/bin/sh", "-c", 'echo "test"')
yield script
task.accept_result(result=await future_result)
  • To the highlighted line here:
script = context.new_script()
script.upload_file("provider.py", "/golem/input/provider.py")
future_result = script.run("/bin/sh", "-c", "python3 /golem/input/provider.py")
yield script
task.accept_result(result=await future_result)

The above changes the command that we run (script.run), from a shell command (echo), to starting a Python script (python3 /golem/input/provider.py).

2 - Creating our provider.py script

In this chapter, we will create our provider.py script and input some example code.

This is the code that we will run on the provider’s machine.

We will go through a mode advanced example later on, which includes downloading files too.

  • Create a new file and name it provider.py.
  • In the file, write something very simple that doesn’t import any modules:
print("HELLO! // PROVIDER :)")
  • Save it.

3 - Testing our application

In this last chapter, we will simply test our application and see if it works.

  • In your command prompt, enter python hello.py .
  • It should output something like this:

What’s next?

Now we have run our own Python script on a provider, but we don’t know how to import our own modules yet. In the following guides you will be:

  • 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

--

--