1- from multiprocessing import cpu_count
2- from multiprocessing .dummy import Pool
3- from typing import Dict , Optional
1+ from typing import Dict , List , Optional
42
53from .auxiliary import AuxiliaryDecrypt
64from .ballot import CiphertextAcceptedBallot , CiphertextSelection
2119from .key_ceremony import ElectionPublicKey
2220from .logs import log_warning
2321from .rsa import rsa_decrypt
22+ from .scheduler import Scheduler
2423from .tally import (
2524 CiphertextTally ,
2625 CiphertextTallyContest ,
@@ -111,17 +110,20 @@ def compute_decryption_share_for_cast_contests(
111110 """
112111 Compute the decryption for all of the cast contests in the Ciphertext Tally
113112 """
114- cpu_pool = Pool (cpu_count ())
115113 contests : Dict [CONTEST_ID , CiphertextDecryptionContest ] = {}
114+ scheduler = Scheduler ()
116115
117116 for contest in tally .cast .values ():
118117 selections : Dict [SELECTION_ID , CiphertextDecryptionSelection ] = {}
119- selection_decryptions = cpu_pool .starmap (
118+ selection_decryptions : List [
119+ Optional [CiphertextDecryptionSelection ]
120+ ] = scheduler .schedule (
120121 compute_decryption_share_for_selection ,
121122 [
122123 (guardian , selection , context )
123124 for (_ , selection ) in contest .tally_selections .items ()
124125 ],
126+ with_shared_resources = True ,
125127 )
126128
127129 # verify the decryptions are received and add them to the collection
@@ -136,7 +138,6 @@ def compute_decryption_share_for_cast_contests(
136138 contests [contest .object_id ] = CiphertextDecryptionContest (
137139 contest .object_id , guardian .object_id , contest .description_hash , selections
138140 )
139- cpu_pool .close ()
140141 return contests
141142
142143
@@ -150,17 +151,20 @@ def compute_compensated_decryption_share_for_cast_contests(
150151 """
151152 Compute the compensated decryption for all of the cast contests in the Ciphertext Tally
152153 """
153- cpu_pool = Pool ( cpu_count () )
154+ scheduler = Scheduler ( )
154155 contests : Dict [CONTEST_ID , CiphertextCompensatedDecryptionContest ] = {}
155156
156157 for contest in tally .cast .values ():
157158 selections : Dict [SELECTION_ID , CiphertextCompensatedDecryptionSelection ] = {}
158- selection_decryptions = cpu_pool .starmap (
159+ selection_decryptions : List [
160+ Optional [CiphertextCompensatedDecryptionSelection ]
161+ ] = scheduler .schedule (
159162 compute_compensated_decryption_share_for_selection ,
160163 [
161164 (guardian , missing_guardian_id , selection , context , decrypt )
162165 for (_ , selection ) in contest .tally_selections .items ()
163166 ],
167+ with_shared_resources = True ,
164168 )
165169
166170 # verify the decryptions are received and add them to the collection
@@ -179,7 +183,6 @@ def compute_compensated_decryption_share_for_cast_contests(
179183 contest .description_hash ,
180184 selections ,
181185 )
182- cpu_pool .close ()
183186 return contests
184187
185188
@@ -189,19 +192,22 @@ def compute_decryption_share_for_spoiled_ballots(
189192 """
190193 Compute the decryption for all spoiled ballots in the Ciphertext Tally
191194 """
192- cpu_pool = Pool (cpu_count ())
193195 spoiled_ballots : Dict [BALLOT_ID , BallotDecryptionShare ] = {}
196+ scheduler = Scheduler ()
194197
195198 for spoiled_ballot in tally .spoiled_ballots .values ():
196199 contests : Dict [CONTEST_ID , CiphertextDecryptionContest ] = {}
197200 for contest in spoiled_ballot .contests :
198201 selections : Dict [SELECTION_ID , CiphertextDecryptionSelection ] = {}
199- selection_decryptions = cpu_pool .starmap (
202+ selection_decryptions : List [
203+ Optional [CiphertextDecryptionSelection ]
204+ ] = scheduler .schedule (
200205 compute_decryption_share_for_selection ,
201206 [
202207 (guardian , selection , context )
203208 for selection in contest .ballot_selections
204209 ],
210+ with_shared_resources = True ,
205211 )
206212 # verify the decryptions are received and add them to the collection
207213 for decryption in selection_decryptions :
@@ -225,7 +231,6 @@ def compute_decryption_share_for_spoiled_ballots(
225231 spoiled_ballot .object_id ,
226232 contests ,
227233 )
228- cpu_pool .close ()
229234 return spoiled_ballots
230235
231236
@@ -239,21 +244,24 @@ def compute_compensated_decryption_share_for_spoiled_ballots(
239244 """
240245 Compute the decryption for all spoiled ballots in the Ciphertext Tally
241246 """
242- cpu_pool = Pool (cpu_count ())
243247 spoiled_ballots : Dict [BALLOT_ID , CompensatedBallotDecryptionShare ] = {}
248+ scheduler = Scheduler ()
244249
245250 for spoiled_ballot in tally .spoiled_ballots .values ():
246251 contests : Dict [CONTEST_ID , CiphertextCompensatedDecryptionContest ] = {}
247252 for contest in spoiled_ballot .contests :
248253 selections : Dict [
249254 SELECTION_ID , CiphertextCompensatedDecryptionSelection
250255 ] = {}
251- selection_decryptions = cpu_pool .starmap (
256+ selection_decryptions : List [
257+ Optional [CiphertextCompensatedDecryptionSelection ]
258+ ] = scheduler .schedule (
252259 compute_compensated_decryption_share_for_selection ,
253260 [
254261 (guardian , missing_guardian_id , selection , context , decrypt )
255262 for selection in contest .ballot_selections
256263 ],
264+ with_shared_resources = True ,
257265 )
258266 # verify the decryptions are received and add them to the collection
259267 for decryption in selection_decryptions :
@@ -279,7 +287,6 @@ def compute_compensated_decryption_share_for_spoiled_ballots(
279287 spoiled_ballot .object_id ,
280288 contests ,
281289 )
282- cpu_pool .close ()
283290 return spoiled_ballots
284291
285292
0 commit comments