Monthly Archives: October 2011

JogaPY : PHP Example

Last night I posted about JogaPY and showed how to use it. Those examples were in python. Today I will show you how to use it using PHP . First you have to download JogaPY_PHP_v1.0.zip . Now extract it.

Client:

[php]

<?php

include_once ‘jogapy/client.php’;

$jogapy = new jogapyclient(‘localhost’,3277);

$jogapy->connect();

$tasks = array();

$tasks[] = new Task(‘echo’,’Hey’);

$tasks[] = new Task(‘echo’,’Baby’);

$jogapy->do_task($tasks);

foreach ($jogapy->result() as $task) {

var_dump($task->result);

}

?>

[/php]

Now save it as my_client.php .

Worker:

[php]

<?php

include_once ‘jogapy/worker.php’;

$worker = new jogapyworker();
$worker->add_server(‘localhost’,3277);
$worker->register_function(‘echo’,’say’);
$worker->work();
function say($arg) {
return $arg;
}
?>

[/php]

Now save it as my_worker.php and run it via php cli. Now run the worker to check the result.

JOGAPY : A Very Simple Job Distribution System

A few days ago  I was learning the use of  Gearman . Then I thought why don’t I try to make a simple job distribution system like it using python. So I developed one and I call it JOGAPY .

Download it.

You can make a job server this way:

[python]
from jogapy.server import *
server = JogapyServer(‘localhost’,3277)
server.run()
[/python]

Now save it as myserver.py . It will start the job server if you execute it.

Here is the code of a client.

[python]
from jogapy.client import *
client = JogapyClient((‘localhost’,3277))
client.do_task([Task('echo','abcde'),Task('echo','fghij'),Task('echo','klmno'),Task('echo','pqrst')])
for result in client.results():
print result.result
[/python]

Save it as myclient.py . Now let’s make a worker.

[python]
from jogapy.worker import *
def echo(say):
return say

worker = JogapyWorker([('localhost',3277)])
worker.register_function(‘echo’,echo)
worker.work()
[/python]

Save the code as myworker.py and execute it.

If you run the myclient.py it will request the job server to process the echo function for it. If any free worker is connected with the server who can process the echo function the job server will tell him to do it. The worker will receive task info and it will start the process. When the task will be complete it will tell the server that the task is complete and it will send the task result to the server. Then the job server will send the task result to the client.