jobmanager/examples/simple_example.py
2015-05-07 00:01:54 +02:00

74 lines
1.8 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import multiprocessing as mp
import numpy as np
from os.path import split, dirname, abspath
import sys
import time
# Add parent directory to beginning of path variable
sys.path = [split(dirname(abspath(__file__)))[0]] + sys.path
import jobmanager as jm
class Example_Client(jm.JobManager_Client):
def __init__(self):
# start quiet client (verbose=0)
super(Example_Client, self).__init__(server="localhost",
authkey='simple example',
verbose=0)
@staticmethod
def func(args, const_args):
"""simply return the current argument"""
return args
class Example_Server(jm.JobManager_Server):
def __init__(self):
# server show status information (verbose=1)
super(Example_Server, self).__init__(authkey='simple example',
verbose=1)
self.final_result = 1
def process_new_result(self, arg, result):
"""over write final_result with the new incoming result
if the new result is smaller then the final_result"""
if self.final_result > result:
self.final_result = result
def process_final_result(self):
print("final_result:", self.final_result)
def run_server():
with Example_Server() as server:
for i in range(5000):
server.put_arg(np.random.rand())
server.start()
def run_client():
client = Example_Client()
client.start()
if __name__ == "__main__":
p_server = mp.Process(target=run_server)
p_server.start()
time.sleep(1)
p_client = mp.Process(target=run_client)
p_client.start()
p_client.join()
p_server.join()