Automate Folding@home: Programmatic Control Guide
Hey there, fellow FAH enthusiasts! Ever wanted to take your Folding@home (FAH) game to the next level? Maybe you're like me and want to squeeze every last bit of processing power out of your rig, even when you're AFK. Well, buckle up, because we're diving deep into the world of programmatically controlling the FAH client. This guide will walk you through the currently supported and recommended methods, helping you automate your client, maximize uptime, and contribute even more to the cause. We'll be focusing on the V8 FAH Client, so if you're still rocking an older version, it might be time for an upgrade!
Understanding the Need for Programmatic Control
So, why bother with programmatic control in the first place? Well, imagine this: you've set up your FAH client, and it's chugging along, contributing to valuable research. But what happens when you step away from your computer? If you're like me, you probably don't want your client to sit idle, wasting precious processing cycles. That's where automation comes in. By programmatically controlling your FAH client, you can implement features like:
- Automatic Resume: Resume your client after periods of inactivity, ensuring continuous contribution.
- Resource Management: Dynamically adjust resource allocation (e.g., CPU cores, GPU usage) based on your needs.
- Monitoring and Reporting: Track your contribution, monitor temperatures, and receive notifications.
- Advanced Scheduling: Create custom schedules for when the client runs, optimizing for off-peak electricity rates or other factors.
- Integration with Smart Home: Control your FAH client alongside other devices in your smart home setup.
The possibilities are endless! But before we get carried away, let's explore how to actually achieve this level of control. We will explore ways you can automate your FAH client, and make sure that you contribute as much as possible to scientific research. Remember guys, this is not just about numbers; it's about making a difference. Let's delve in!
The Recommended Methods: APIs and Interfaces
Alright, let's get down to brass tacks. How do you actually talk to the FAH client? Fortunately, the developers have provided several methods for programmatic control. Here are the currently supported and recommended options:
-
The FAHClient API (JSON-RPC): This is the go-to method for most users. The FAH client exposes a JSON-RPC (Remote Procedure Call) API, allowing you to send commands and receive data over a network connection. This is the most flexible and feature-rich option. You can use any programming language that supports HTTP requests and JSON parsing (which is pretty much all of them). The API allows you to:
- Control Client State: Start, stop, and pause the client.
- Manage Work Units: Get information about the current work unit, its progress, and estimated time remaining.
- Configure Client Settings: Adjust CPU usage, GPU settings, and other configuration options.
- Monitor System Stats: Retrieve real-time information about your CPU and GPU usage, temperatures, and power consumption.
To use the FAHClient API, you'll typically send JSON-formatted requests to the client's web interface (usually running on port 7396). The requests contain the method you want to call and any required parameters. The client then responds with JSON-formatted data. The API is well documented, so you should be able to look up all the methods and the parameter that are required.
-
Command-Line Interface (CLI): If you prefer a more direct approach, the FAH client also offers a command-line interface. This is useful for simple automation tasks or for scripting. You can use the
fah-clientexecutable to send commands to the client. The CLI commands include:start: Starts the client.stop: Stops the client.pause: Pauses the client.resume: Resumes the client.config: Modify the configuration settings.
The CLI is generally less feature-rich than the JSON-RPC API, but it's easier to get started with for basic tasks. You can execute these commands directly from your terminal or include them in a batch script or a Python script.
-
Local Interface (Deprecated, but still works): Older versions of the FAH client used a local interface for control. This involved sending commands to the client via a named pipe. This method is deprecated and not recommended for new projects, but it might still work if you're using an older FAH client. Since it is deprecated, this guide will not focus on this.
Getting Started with the JSON-RPC API in Python
Let's get our hands dirty with a practical example. We will use Python, as requested, to control the FAH client using the JSON-RPC API. Here's a basic script to resume the client after a period of inactivity. This is a simple example to show how to control the FAH client.
import json
import requests
import time
# FAHClient API endpoint
API_ENDPOINT = "http://localhost:7396/jsonrpc"
# Function to send a command to the FAH client
def send_command(method, params=None):
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params or [],
"id": 1
}
try:
response = requests.post(API_ENDPOINT, json=payload, timeout=5)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error sending command: {e}")
return None
# Function to check the client status
def get_status():
response = send_command("client-status")
if response and "result" in response:
return response["result"]
return None
# Function to resume the client
def resume_client():
response = send_command("client-control", ["resume"])
if response and "result" in response and response["result"] == "OK":
print("Client resumed.")
else:
print("Failed to resume client.")
# Main loop
if __name__ == "__main__":
idle_time_threshold = 600 # seconds (10 minutes)
while True:
status = get_status()
if status:
if "paused" in status and status["paused"] == True:
resume_client()
time.sleep(idle_time_threshold)
Let's break down what's happening here:
- Imports: We import the necessary modules:
jsonfor handling JSON data,requestsfor making HTTP requests to interact with the FAH client API, andtimeto introduce pauses in our script. - API Endpoint: We define the
API_ENDPOINT, which is the URL where the FAH client's API is exposed. By default, it'shttp://localhost:7396/jsonrpc. Ensure your FAH client is running and configured to allow remote connections (check theallow-remotesetting in yourconfig.xmlfile, and set it totrue). send_commandFunction: This function handles sending commands to the FAH client. It takes the method name and any parameters as input, formats them into a JSON-RPC request, sends the request via an HTTP POST, and returns the response as JSON. It also includes error handling usingtry...exceptto catch potential issues during the request.get_statusFunction: This function queries the FAH client for its current status using the `