ソースを参照

Shared manager with redis

Bastien Sevajol 6 年 前
コミット
dd7ad18ffd
共有3 個のファイルを変更した23 個の追加8 個の削除を含む
  1. 3 1
      requirements.txt
  2. 7 4
      synergine2/share.py
  3. 13 3
      tests/test_processing.py

+ 3 - 1
requirements.txt ファイルの表示

13
 pyparsing==2.1.10
13
 pyparsing==2.1.10
14
 pytest==3.0.4
14
 pytest==3.0.4
15
 pytest-cov==2.5.1
15
 pytest-cov==2.5.1
16
+pytest-timeout==1.2.0
16
 pytest-xdist==1.16.0
17
 pytest-xdist==1.16.0
17
 PyYAML==3.12
18
 PyYAML==3.12
19
+redis==2.10.6
18
 six==1.10.0
20
 six==1.10.0
21
+tmx==1.9.1
19
 typing==3.6.1
22
 typing==3.6.1
20
-pylibmc==1.5.2

+ 7 - 4
synergine2/share.py ファイルの表示

1
 # coding: utf-8
1
 # coding: utf-8
2
+import pickle
2
 import typing
3
 import typing
3
 
4
 
4
-import pylibmc
5
+import redis
5
 
6
 
6
 from synergine2.exceptions import SynergineException
7
 from synergine2.exceptions import SynergineException
7
 
8
 
12
     start of processes. Processes will only be able to access shared memory filled here before start.
13
     start of processes. Processes will only be able to access shared memory filled here before start.
13
     """
14
     """
14
     def __init__(self):
15
     def __init__(self):
15
-        self._mc = pylibmc.Client(['127.0.0.1'], binary=True, behaviors={"tcp_nodelay": True, "ketama": True})
16
+        self._r = redis.StrictRedis(host='localhost', port=6379, db=0)  # TODO: configs
17
+        # TODO: Il faut écrire dans REDIS que lorsque l'on veut passer à l'étape processes, genre de commit
18
+        # sinon on va ecrire dans redis a chaque fois qu'on modifie une shared data c'est pas optimal.
16
 
19
 
17
     def set(self, key: str, value: typing.Any) -> None:
20
     def set(self, key: str, value: typing.Any) -> None:
18
-        self._mc.set(key, value)
21
+        self._r.set(key, pickle.dumps(value))
19
 
22
 
20
     def get(self, key) -> typing.Any:
23
     def get(self, key) -> typing.Any:
21
-        return self._mc.get(key)
24
+        return pickle.loads(self._r.get(key))
22
 
25
 
23
     def create(
26
     def create(
24
         self,
27
         self,

+ 13 - 3
tests/test_processing.py ファイルの表示

21
 
21
 
22
 
22
 
23
 class TestProcessing(BaseTest):
23
 class TestProcessing(BaseTest):
24
+    @pytest.mark.timeout(10)
24
     def make_job_with_scalar(
25
     def make_job_with_scalar(
25
             self,
26
             self,
26
             data: list,
27
             data: list,
28
         result = sum(data)
29
         result = sum(data)
29
         return result
30
         return result
30
 
31
 
32
+    @pytest.mark.timeout(10)
31
     def make_job_with_object(
33
     def make_job_with_object(
32
             self,
34
             self,
33
             data: list,
35
             data: list,
50
 
52
 
51
         assert sum(results) == 39600
53
         assert sum(results) == 39600
52
 
54
 
55
+    @pytest.mark.timeout(10)
53
     def test_non_parallel_jobs_with_scalar(self):
56
     def test_non_parallel_jobs_with_scalar(self):
54
         # TODO: process manager utilise actuellement un cpu quand même, changer ca
57
         # TODO: process manager utilise actuellement un cpu quand même, changer ca
55
         process_manager = ProcessManager(
58
         process_manager = ProcessManager(
66
         assert len(results) == 1
69
         assert len(results) == 1
67
         assert final_result == 4950
70
         assert final_result == 4950
68
 
71
 
72
+    @pytest.mark.timeout(10)
69
     def test_parallel_jobs_with_objects(self):
73
     def test_parallel_jobs_with_objects(self):
70
         process_manager = ProcessManager(
74
         process_manager = ProcessManager(
71
             config=Config({}),
75
             config=Config({}),
84
 
88
 
85
         assert final_result == 39600
89
         assert final_result == 39600
86
 
90
 
91
+    @pytest.mark.timeout(10)
87
     def test_shared_memory_with_shared_manager(self):
92
     def test_shared_memory_with_shared_manager(self):
88
         shared = SharedDataManager()
93
         shared = SharedDataManager()
89
         shared.set('counter', 42)
94
         shared.set('counter', 42)
90
 
95
 
91
         def job(*args, **kwargs):
96
         def job(*args, **kwargs):
92
-            return shared.get('counter') + 1
97
+            counter = shared.get('counter') or 0
98
+            return counter + 1
93
 
99
 
94
         process_manager = ProcessManager(
100
         process_manager = ProcessManager(
95
             config=Config({}),
101
             config=Config({}),
102
 
108
 
103
         assert results[0] == 43
109
         assert results[0] == 43
104
 
110
 
111
+    @pytest.mark.timeout(10)
105
     def test_share_data_with_function(self):
112
     def test_share_data_with_function(self):
106
         shared = SharedDataManager()
113
         shared = SharedDataManager()
107
 
114
 
109
             counter = shared.create('counter', 0)
116
             counter = shared.create('counter', 0)
110
 
117
 
111
         def job(*args, **kwargs):
118
         def job(*args, **kwargs):
112
-            return shared.get('counter') + 1
119
+            counter = shared.get('counter') or 0
120
+            return counter + 1
113
 
121
 
114
         process_manager = ProcessManager(
122
         process_manager = ProcessManager(
115
             config=Config({}),
123
             config=Config({}),
130
 
138
 
131
         process_manager.terminate()
139
         process_manager.terminate()
132
 
140
 
141
+    @pytest.mark.timeout(10)
133
     def test_after_created_shared_data(self):
142
     def test_after_created_shared_data(self):
134
         shared = SharedDataManager()
143
         shared = SharedDataManager()
135
 
144
 
136
         shared.set('foo_1', 0)
145
         shared.set('foo_1', 0)
137
 
146
 
138
         def job(key):
147
         def job(key):
139
-            return shared.get('foo_{}'.format(key)) + 1
148
+            value = shared.get('foo_{}'.format(key)) or 0
149
+            return value + 1
140
 
150
 
141
         process_manager = ProcessManager(
151
         process_manager = ProcessManager(
142
             config=Config({}),
152
             config=Config({}),