iron_worker_python is Python language binding for IronWorker.
IronWorker is a massively scalable background processing system. See How It Works
To start using iron_worker_python, you need to sign up and get an oauth token.
- Go to http://iron.io/ and sign up.
- Get an OAuth Token at http://hud.iron.io/tokens
Just copy iron_worker.py
and poster
and include iron_worker.py in your script:
from iron_worker import *
Two ways to configure IronWorker:
- Specifying options when constructing the binding:
worker = IronWorker(token='XXXXXXXXXX', project_id='xxxxxxxxxxx')
- Passing an ini file name which stores your configuration options. Rename sample_config.ini to config.ini and include your Iron.io credentials (
token
andproject_id
):
worker = IronWorker(config='config.ini')
Here's an example worker:
print "Hello PYTHON World!\n"
- Zip worker:
# Zip single file:
IronWorker.createZip(destination="worker.zip", files=['HelloWorld.py'], overwrite=True)
# OR
# Zip whole directory:
IronWorker.zipDirectory(directory="hello_world/", destination='worker.zip', overwrite=True)
- Submit worker:
res = worker.postCode(runFilename='HelloWorld.py', zipFilename='worker.zip', name='HelloWorld')
Where 'HelloWorld' is a worker name which should be used later for queueing and sheduling.
task = worker.postTask(name='HelloWorld')
Worker should start in a few seconds.
If you want to run your code at a delay, you should schedule it:
If you want to run your code repeatedly, you can create a Scheduled Task:
# run every hour, five times in total
run_every = 60*60 # 60 seconds in a minute, 60 minutes in an hour
worker.postSchedule(name='HelloWorld', run_every=run_every, run_times=5)
To get the status of a worker, you can use the getTaskDetails()
method.
task = worker.postTask('HelloWorld')
details = worker.getTaskDetails(task_id=task['tasks'][0]['id']);
print details['status'] # prints 'queued', 'complete', 'error' etc.
Use any function that print text inside your worker to put messages to log.
import time
task = worker.postTask('HelloWorld')
time.sleep(10)
details = worker.getTaskDetails(task['tasks'][0]['id'])
# Check log only if task is finished.
if details['status'] != 'queued':
log = worker.getLog(task_id);
print log # prints "Hello PHP World!"
To provide Payload to your worker simply put a dict with any content you want.
payload = {
'key_one': 'Helpful text',
'key_two': 2,
'options': ['option 1', 'option 2']
}
worker.postTask(name='HelloWorld', payload=payload)
When your code is executed, it will be passed three program arguments:
- -id - The task id.
- -payload - the filename containing the data payload for this particular task.
- -d - the user writable directory that can be used while running your job.
You can find more documentation here:
- Iron.io Dev Center: Full documentation for Iron.io products.
- Example Workers
- IronWorker Python Wiki pages.