-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an asynchronous grading option using asyncio.
- Loading branch information
Showing
2 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import asyncio | ||
import concurrent.futures | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def grade(assignment, submission, user_id, rebuild=False): | ||
""" | ||
Grade a single submission asyncronously. | ||
""" | ||
logger.info("Grading submission for %s", user_id) | ||
submission.grade(assignment, rebuild_container=rebuild, | ||
show_output=False) | ||
|
||
|
||
def async_grade(args, assignment, users): | ||
""" | ||
Asynchronously grade submissions. | ||
:param argparse.Arguments args: the arguments from the grade | ||
comand. | ||
:param Assignment assignment: the assignment for which to grade | ||
submissions. | ||
:param dict users: user_id: [Submission, ...], all | ||
submissions will be graded. | ||
""" | ||
async def run_blocking_tasks(executor): | ||
loop = asyncio.get_event_loop() | ||
blocking_tasks = [] | ||
|
||
logger.debug("Creating work queue.") | ||
for user_id, submissions in users.items(): | ||
for submission in submissions: | ||
blocking_tasks.append( | ||
loop.run_in_executor( | ||
executor, grade, assignment, submission, user_id, | ||
args.rebuild)) | ||
|
||
logger.debug("Waiting on pool to finish.") | ||
await asyncio.wait(blocking_tasks) | ||
|
||
executor = concurrent.futures.ProcessPoolExecutor( | ||
max_workers=int(args.j) | ||
) | ||
event_loop = asyncio.get_event_loop() | ||
|
||
try: | ||
event_loop.run_until_complete( | ||
run_blocking_tasks(executor) | ||
) | ||
finally: | ||
event_loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters