def _handle_recv_back(self, msg): # do the job and send the result if self.debug: logger.debug('Job received') target = timed()(self.target) else: target = self.target duration = -1 # results are sent with a PID:OK: or a PID:ERROR prefix try: with self.timer.run_job(): res = target(Job.load_from_string(msg[0])) # did we timout ? if self.timer.timed_out: # let's dump the last for line in self.timer.last_dump: logger.error(line) if self.debug: duration, res = res res = '%d:OK:%s' % (self.pid, res) except Exception, e: exc_type, exc_value, exc_traceback = sys.exc_info() exc = traceback.format_tb(exc_traceback) exc.insert(0, str(e)) res = '%d:ERROR:%s' % (self.pid, '\n'.join(exc)) logger.error(res)
def _execute(self, job, timeout=None): if isinstance(job, str): job = Job(job) if timeout is None: timeout = self.timeout_max_overflow with self.lock: send(self.master, job.serialize()) while True: try: socks = dict(self.poller.poll(timeout)) break except zmq.ZMQError as e: if e.errno != errno.EINTR: raise if socks.get(self.master) == zmq.POLLIN: return extract_result(recv(self.master)) raise TimeoutError(timeout / 1000)
def application(environ, start_response): status = '200 OK' headers = [('Content-type', 'text/html')] start_response(status, headers) data = {} for key, value in environ.items(): if key.startswith('wsgi.') or key.startswith('gunicorn.'): continue data[key] = value job = Job(json.dumps(data)) start = time.time() try: return client.execute(job) finally: print('Time : %.4f\n' % (time.time() - start))
def _handle_recv_back(self, msg): # do the job and send the result if self.debug: logger.debug("Job received") target = timed()(self.target) else: target = self.target duration = -1 # results are sent with a PID:OK: or a PID:ERROR prefix riemann_message = { "host": socket.gethostname(), "service": "powerhose-worker", "metric": 1, "state": "ok", "tags": ["running"], } try: with self.timer.run_job(): self.riemann.send(riemann_message) res = target(Job.load_from_string(msg[0])) # did we timout ? if self.timer.timed_out: # let's dump the last riemann_message["state"] = "error" riemann_message["tags"] = ["timeouts"] self.riemann.send(riemann_message) for line in self.timer.last_dump: logger.error(line) if self.debug: duration, res = res res = "%d:OK:%s" % (self.pid, res) except Exception, e: riemann_message["description"] = str(e) riemann_message["state"] = "error" riemann_message["tags"] = ["faults"] exc_type, exc_value, exc_traceback = sys.exc_info() exc = traceback.format_tb(exc_traceback) exc.insert(0, str(e)) res = "%d:ERROR:%s" % (self.pid, "\n".join(exc)) logger.error(res)
def run(self): i = 0 self.running = True while self.running: data = str(random.randint(1, 1000)) job = Job(data) sys.stdout.write(str(i) + '-> ') sys.stdout.flush() try: res = self.pool.execute(job) except NoWorkerError: sys.stdout.write('NO WORKER\n') else: assert res == data sys.stdout.write('OK\n') sys.stdout.flush() i += 1
def test_job(self): job = Job('somedata', {'one': '1'}) data = job.serialize() job2 = Job.load_from_string(data) self.assertTrue(job.data, job2.data) self.assertTrue(job.headers.items(), job2.headers.items())
def patched_runner(job): return self.runner(Job(job))
import random import sys import time from powerhose.client import Client from powerhose.job import Job from powerhose.util import set_logger set_logger(True) client = Client() print 'Running 100 echo' start = time.time() for i in range(2000): data = str(random.randint(1, 1000)) job = Job(data) res = client.execute(job) assert res == data sys.stdout.write('.') sys.stdout.flush() print 'Done in %.2f' % (time.time() - start) import pdb pdb.set_trace()
def run(): data = binascii.b2a_hex(os.urandom(256))[:256] job = Job(data + '--' + random.choice(algs)) return client.execute(job)