Example #1
0
    def _execute_task_with_cache(self, bear, task):
        if type(bear) not in self.cache:
            bear_cache = {}
            self.cache[type(bear)] = bear_cache
        else:
            bear_cache = self.cache[type(bear)]

        fingerprint = persistent_hash(task)

        if fingerprint in bear_cache:
            results = bear_cache[fingerprint]
        else:
            bear_args, bear_kwargs = task

            future = asyncio.run_coroutine_threadsafe(asyncio.wait_for(
                self.event_loop.run_in_executor(self.executor,
                                                bear.execute_task, bear_args,
                                                bear_kwargs),
                None,
                loop=self.event_loop),
                                                      loop=self.event_loop)

            results = future.result()
            bear_cache[fingerprint] = results

        return results
Example #2
0
 def test_nested_tuples_and_dicts(self):
     self.assertEqual(
         persistent_hash((('a', 'b', {'g', 'b', '8'},
                           {'g': '1', 'a': {}, 'b': '8'}),
                          {'q': {'g': '1', 'a': '1'}, 'a': {},
                           'g': {'z', 'd'}, 'b': '8'})),
         b'\xa9z[U\xfa\xd1x\x95\x00\xf1,h%Y\xa2u\x87\xb0\xb2\x13')
Example #3
0
    def _execute_task_with_cache(self, bear, task):
        if type(bear) not in self.cache:
            bear_cache = {}
            self.cache[type(bear)] = bear_cache
        else:
            bear_cache = self.cache[type(bear)]

        fingerprint = persistent_hash(task)

        if fingerprint in bear_cache:
            results = bear_cache[fingerprint]
        else:
            bear_args, bear_kwargs = task

            future = asyncio.run_coroutine_threadsafe(
                asyncio.wait_for(
                    self.event_loop.run_in_executor(
                        self.executor, bear.execute_task,
                        bear_args, bear_kwargs),
                    None,
                    loop=self.event_loop),
                loop=self.event_loop)

            results = future.result()
            bear_cache[fingerprint] = results

        return results
Example #4
0
 def test_tuple_with_sets_and_dicts(self):
     self.assertEqual(
         persistent_hash((({'g', 'a'}, {
             'e': '7',
             'a': '1'
         }), {})),
         b'xvr\xa0\xd3\x9c\x125\x12H\xcf\x13\xc4\xba\xf5\x15Hz\xe2\x00')
Example #5
0
 def test_nested_dicts(self):
     self.assertEqual(
         persistent_hash(((), {
             'q': {
                 'g': '1',
                 'b': '8'
             },
             'a': {},
             'g': {'z', 'c', 'd'},
             'b': '8'
         })), b'\xdaI\xb5S(wb\xd7\x82\xa0\x17cV-}\x1cL\x9d\xc1a')
Example #6
0
 def test_nested_tuples_and_dicts(self):
     self.assertEqual(
         persistent_hash((('a', 'b', {'g', 'b', '8'}, {
             'g': '1',
             'a': {},
             'b': '8'
         }), {
             'q': {
                 'g': '1',
                 'a': '1'
             },
             'a': {},
             'g': {'z', 'd'},
             'b': '8'
         })), b'\xa9z[U\xfa\xd1x\x95\x00\xf1,h%Y\xa2u\x87\xb0\xb2\x13')
Example #7
0
 def test_int(self):
     self.assertEqual(
         persistent_hash(3),
         b'\xd8YA\x03x|c"@\xe8\x8b~\xb9\xb6\x8d\x95\x8dzp\x8a')
Example #8
0
 def test_int_tuples(self):
     self.assertEqual(
         persistent_hash((1, 2, 3)),
         b'\xb5\xd6\xd7\xbeLD\x90\x9fz.\xae\xc4\xb9P\n\xf8\xf5\x03S\xb6')
Example #9
0
 def test_plain_dicts(self):
     self.assertEqual(
         persistent_hash({'a': '1', 'g': '9'}),
         b'D\xe8NL!\x8c\xec\xff\xf3\x93\xee=0K#\x1fVL\x06$')
Example #10
0
 def test_dicts(self):
     self.assertEqual(
         persistent_hash(((), {
             'a': '1',
             'g': '9'
         })), b'\x99E\xday\xd3\xc0;S\xb7\x01\xb1\xd6>F\xdb\xc3y\x12\xf7W')
Example #11
0
 def test_plain_dicts(self):
     self.assertEqual(persistent_hash({
         'a': '1',
         'g': '9'
     }), b'D\xe8NL!\x8c\xec\xff\xf3\x93\xee=0K#\x1fVL\x06$')
Example #12
0
 def test_sets(self):
     self.assertEqual(
         persistent_hash({'a', '1', 'g'}),
         b'\xbc\x99\x95\x97#F\x13<8\x1cK\x81\xf2KxQ@\xf6\x01%')
Example #13
0
 def test_nested_dicts(self):
     self.assertEqual(
         persistent_hash(((), {'q': {'g': '1', 'b': '8'}, 'a': {},
                               'g': {'z', 'c', 'd'}, 'b': '8'})),
         b'\xdaI\xb5S(wb\xd7\x82\xa0\x17cV-}\x1cL\x9d\xc1a')
Example #14
0
 def test_tuple_with_sets_and_dicts(self):
     self.assertEqual(
         persistent_hash((({'g', 'a'}, {'e': '7', 'a': '1'}), {})),
         b'xvr\xa0\xd3\x9c\x125\x12H\xcf\x13\xc4\xba\xf5\x15Hz\xe2\x00')
Example #15
0
 def test_dicts(self):
     self.assertEqual(
         persistent_hash(((), {'a': '1', 'g': '9'})),
         b'\x99E\xday\xd3\xc0;S\xb7\x01\xb1\xd6>F\xdb\xc3y\x12\xf7W')
Example #16
0
 def test_int_tuples(self):
     self.assertEqual(
         persistent_hash((1, 2, 3)),
         b'\xb5\xd6\xd7\xbeLD\x90\x9fz.\xae\xc4\xb9P\n\xf8\xf5\x03S\xb6')
Example #17
0
 def test_int(self):
     self.assertEqual(
         persistent_hash(3),
         b'\xd8YA\x03x|c"@\xe8\x8b~\xb9\xb6\x8d\x95\x8dzp\x8a')
Example #18
0
 def test_sets(self):
     self.assertEqual(
         persistent_hash({'a', '1', 'g'}),
         b'\xbc\x99\x95\x97#F\x13<8\x1cK\x81\xf2KxQ@\xf6\x01%')