1
1
import dramatiq
2
+ import multiprocessing
2
3
3
4
from account .models import User , UserProfile
4
5
from submission .models import Submission
8
9
from .dispatcher import JudgeDispatcher
9
10
from .testing import SubmissionTester
10
11
12
+ lock = multiprocessing .Lock ()
11
13
12
14
@dramatiq .actor (** DRAMATIQ_WORKER_ARGS ())
13
15
def judge_task (submission_id , problem_id ):
@@ -19,33 +21,41 @@ def judge_task(submission_id, problem_id):
19
21
20
22
@dramatiq .actor (** DRAMATIQ_WORKER_ARGS ())
21
23
def local_judge_task (submission_id , problem_id , user_id ):
24
+ global lock
22
25
submission = Submission .objects .get (id = submission_id )
23
26
24
- problem = Problem .objects .get (id = problem_id )
25
- problem .submission_number += 1
27
+ with lock :
28
+ problem = Problem .objects .get (id = problem_id )
29
+ problem .submission_number += 1
30
+ problem .save ()
31
+ judge_res = False
26
32
try :
27
- if SubmissionTester (submission ).judge ():
28
- problem .accepted_number += 1
33
+ judge_res = SubmissionTester (submission ).judge ()
29
34
except Exception as e :
30
35
raise
31
- problem .save ()
36
+ with lock :
37
+ problem = Problem .objects .get (id = problem_id )
38
+ if judge_res :
39
+ problem .accepted_number += 1
40
+ problem .save ()
32
41
33
42
score = submission .grade
34
43
user = User .objects .get (id = user_id )
35
44
assert user
36
- profile = UserProfile .objects .get (user = user )
37
- assert profile
38
- profile .total_submissions += 1
39
- if problem ._id not in profile .problems_status :
40
- profile .problems_status [problem ._id ] = score
41
- profile .total_score += score
42
- if score == 100 :
43
- profile .accepted_number += 1
44
- else :
45
- prev_score = profile .problems_status [problem ._id ]
46
- if score > prev_score :
45
+ with lock :
46
+ profile = UserProfile .objects .get (user = user )
47
+ assert profile
48
+ profile .total_submissions += 1
49
+ if problem ._id not in profile .problems_status :
47
50
profile .problems_status [problem ._id ] = score
48
- profile .total_score += ( score - prev_score )
51
+ profile .total_score += score
49
52
if score == 100 :
50
53
profile .accepted_number += 1
51
- profile .save ()
54
+ else :
55
+ prev_score = profile .problems_status [problem ._id ]
56
+ if score > prev_score :
57
+ profile .problems_status [problem ._id ] = score
58
+ profile .total_score += (score - prev_score )
59
+ if score == 100 :
60
+ profile .accepted_number += 1
61
+ profile .save ()
0 commit comments