11import dramatiq
2+ import multiprocessing
23
34from account .models import User , UserProfile
45from submission .models import Submission
89from .dispatcher import JudgeDispatcher
910from .testing import SubmissionTester
1011
12+ lock = multiprocessing .Lock ()
1113
1214@dramatiq .actor (** DRAMATIQ_WORKER_ARGS ())
1315def judge_task (submission_id , problem_id ):
@@ -19,33 +21,41 @@ def judge_task(submission_id, problem_id):
1921
2022@dramatiq .actor (** DRAMATIQ_WORKER_ARGS ())
2123def local_judge_task (submission_id , problem_id , user_id ):
24+ global lock
2225 submission = Submission .objects .get (id = submission_id )
2326
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
2632 try :
27- if SubmissionTester (submission ).judge ():
28- problem .accepted_number += 1
33+ judge_res = SubmissionTester (submission ).judge ()
2934 except Exception as e :
3035 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 ()
3241
3342 score = submission .grade
3443 user = User .objects .get (id = user_id )
3544 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 :
4750 profile .problems_status [problem ._id ] = score
48- profile .total_score += ( score - prev_score )
51+ profile .total_score += score
4952 if score == 100 :
5053 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