jobmanager/examples/simple_example.py

75 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import multiprocessing as mp
import numpy as np
2014-12-29 11:42:52 +01:00
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
2014-09-24 13:23:36 +02:00
import jobmanager as jm
2014-12-29 11:42:52 +01:00
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)
2014-12-29 11:42:52 +01:00
def run_server():
with Example_Server() as server:
for i in range(5000):
server.put_arg(np.random.rand())
server.start()
2014-12-29 11:42:52 +01:00
def run_client():
client = Example_Client()
client.start()
2014-12-29 11:42:52 +01:00
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()