def testMcas(self):
   mc = GetMemcacheClient()
   key1, key2 = uuid4().hex, uuid4().hex
   mc.set_multi({key1: 'foo', key2: 'bar'})
   item1, item2 = mcas_get(mc, key1), mcas_get(mc, key2)
   self.assertTrue(mcas(mc, [(item1, 'foo2'), (item2, 'bar2')]))
   self.assertEqual(mc.get_multi([key1, key2]), {key1: 'foo2', key2: 'bar2'})
 def testMcas(self):
   mc = GetMemcacheClient()
   key1, key2 = uuid4().hex, uuid4().hex
   mc.set_multi({key1: 'foo', key2: 'bar'})
   item1, item2 = mcas_get(mc, key1), mcas_get(mc, key2)
   self.assertTrue(mcas(mc, [(item1, 'foo2'), (item2, 'bar2')]))
   self.assertEqual(mc.get_multi([key1, key2]), {key1: 'foo2', key2: 'bar2'})
 def testMcasGet(self):
   mc = GetMemcacheClient()
   key = uuid4().hex
   value = 'foo'
   self.assertTrue(mc.set(key, value))
   item = mcas_get(mc, key)
   self.assertEqual(key, item.key)
   self.assertEqual(value, item.value)
   self.assertTrue(mc.set(key, 'bar'))
   item2 = mcas_get(mc, key)
   self.assertNotEqual(item.cas_id, item2.cas_id)
 def testMcasGet(self):
     mc = GetMemcacheClient()
     key = uuid4().hex
     value = 'foo'
     self.assertTrue(mc.set(key, value))
     item = mcas_get(mc, key)
     self.assertEqual(key, item.key)
     self.assertEqual(value, item.value)
     self.assertTrue(mc.set(key, 'bar'))
     item2 = mcas_get(mc, key)
     self.assertNotEqual(item.cas_id, item2.cas_id)
def worker():
  QueueManager.register(COMMAND_QUEUE)
  m = QueueManager(address=('', CONTROL_PORT), authkey=CONTROL_KEY)
  m.connect()
  command_queue = getattr(m, COMMAND_QUEUE)()
  r = Random()
  r.seed(time.time())

  while True:
    command = command_queue.get()
    mc = GetMemcacheClient(command['servers'])
    location_keys = command['location_keys']
    num_locations_per_mcas = command['num_locations_per_mcas']
    batch_size = command['batch_size']
    print('worker received command')
    batch_time = time.time()
    num_retries = 0
    for i in range(batch_size):
      op_time = time.time()
      mcas_keys = r.sample(location_keys, num_locations_per_mcas)
      while True:
        if mcas(mc, ((item, item.value + 1)
            for item in (mcas_get(mc, key) for key in mcas_keys))):
          break
        else:
          num_retries += 1
      if TARGET_RATE:
        time.sleep(max(0, 1 / TARGET_RATE - (time.time() - op_time)))
    print('done: %.1f operations per second, %s retries' % (
         batch_size/(time.time() - batch_time), num_retries))
    command_queue.task_done()
Пример #6
0
def worker():
    QueueManager.register(COMMAND_QUEUE)
    m = QueueManager(address=('', CONTROL_PORT), authkey=CONTROL_KEY)
    m.connect()
    command_queue = getattr(m, COMMAND_QUEUE)()
    r = Random()
    r.seed(time.time())

    while True:
        command = command_queue.get()
        mc = GetMemcacheClient(command['servers'])
        location_keys = command['location_keys']
        num_locations_per_mcas = command['num_locations_per_mcas']
        batch_size = command['batch_size']
        print('worker received command')
        batch_time = time.time()
        num_retries = 0
        for i in range(batch_size):
            op_time = time.time()
            mcas_keys = r.sample(location_keys, num_locations_per_mcas)
            while True:
                if mcas(mc, ((item, item.value + 1)
                             for item in (mcas_get(mc, key)
                                          for key in mcas_keys))):
                    break
                else:
                    num_retries += 1
            if TARGET_RATE:
                time.sleep(max(0, 1 / TARGET_RATE - (time.time() - op_time)))
        print('done: %.1f operations per second, %s retries' %
              (batch_size / (time.time() - batch_time), num_retries))
        command_queue.task_done()