Skip to content

Commit 39beec2

Browse files
committed
fix(judge): use lock in testing
1 parent 157d5e3 commit 39beec2

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

judge/tasks.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dramatiq
2+
import multiprocessing
23

34
from account.models import User, UserProfile
45
from submission.models import Submission
@@ -8,6 +9,7 @@
89
from .dispatcher import JudgeDispatcher
910
from .testing import SubmissionTester
1011

12+
lock = multiprocessing.Lock()
1113

1214
@dramatiq.actor(**DRAMATIQ_WORKER_ARGS())
1315
def judge_task(submission_id, problem_id):
@@ -19,33 +21,41 @@ def judge_task(submission_id, problem_id):
1921

2022
@dramatiq.actor(**DRAMATIQ_WORKER_ARGS())
2123
def 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()

manage

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function run_onl {
4242
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
4343
nvm use v8.12.0
4444
workdir=$(pwd)
45-
process_num=$(($(nproc) * 2 - 1))
45+
process_num=$(nproc)
46+
echo "starting ${process_num} procs"
4647

4748
# run onl
4849
if ! command pgrep -f "$supervisor_cmd" > /dev/null; then

0 commit comments

Comments
 (0)