mycloud / src / mycloud /

#!/usr/bin/env python

from cloud.serialization import cloudpickle
from mycloud.util import XMLServer
import argparse
import cPickle
import logging
import mycloud.thread
import mycloud.util
import os
import select
import socket
import sys
import time
import xmlrpclib


__doc__ = '''Worker for executing cluster tasks.'''

def watchdog(worker):
  while 1:
    r, w, x =[sys.stdin], [], [sys.stdin], 1)
    if r or x:'Lost controller.  Exiting.')

class Worker(XMLServer):
  def __init__(self, *args, **kw):
    XMLServer.__init__(self, *args, **kw) = socket.gethostname()
    self.port = self.server_address[1]
    self.last_keepalive = time.time()'Worker starting on %s:%s',, self.port)

  def execute_task(self, pickled):
      f, args, kw = cPickle.loads('Executing task %s %s %s', f, args, kw)
      result = f(*args, **kw)
      dump = cloudpickle.dumps(result)'Got result!')
      return xmlrpclib.Binary(dump)
    except:'Failed to execute task.', exc_info=1)

  def healthcheck(self):
    self.last_keepalive = time.time()
    return 'alive'

if __name__ == '__main__':
  p = argparse.ArgumentParser()
  p.add_argument('--index', type=int)
  p.add_argument('--logger_host', type=str)
  p.add_argument('--logger_port', type=int)
  p.add_argument('--worker_name', type=str, default='worker')

  opts = p.parse_args()

  index = opts.index
  myport = mycloud.util.find_open_port()

  log_prefix = '/tmp/%s-worker-%03d' % (socket.gethostname(), index)

  logging.basicConfig(stream=open(log_prefix + '.log', 'w'),
                      format='%(asctime)s %(filename)s:%(funcName)s %(message)s',

  if opts.logger_host:'Additionally logging to %s:%s',
                 opts.logger_host, opts.logger_port)

      logging.handlers.DatagramHandler(opts.logger_host, opts.logger_port))

  worker = Worker(('', myport))
  worker.timeout = 1

  print myport

  # redirect stdout and stderr to local files to avoid pipe/buffering issues
  # with controller 
  sys.stdout = open(log_prefix + '.out', 'w')
  sys.stderr = open(log_prefix + '.err', 'w')

  mycloud.thread.spawn(watchdog, worker)

  # handle requests until we lose our stdin connection the controller
    while 1:
  except:'Error while serving.', exc_info=1)'Shutting down.')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.