Created
May 1, 2019 13:44
-
-
Save bennettscience/eca29a3f1840d3cffb8b55dc5b9d1784 to your computer and use it in GitHub Desktop.
First take at using multiprocessing with UCFOpen's canvasapi in Python
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
import multiprocessing as mp | |
from canvasapi import Canvas | |
import json | |
def process_submissions(assignment): | |
obj = {} | |
canvas = Canvas(url, key) | |
course = canvas.get_course(28869) | |
assignment = course.get_assignment(assignment) | |
obj['id'] = assignment.id | |
obj['sub_data'] = [] | |
submissions = assignment.get_submissions(include='user') | |
for sub in submissions: | |
data = json.loads(sub.to_json()) | |
obj['sub_data'].append((data['user_id'], data['score'])) | |
return obj | |
def main(): | |
# Connect with Canvas | |
canvas = Canvas(url, key) | |
# Get the course object | |
course = canvas.get_course(course_id) | |
assignments = course.get_assignments() | |
# Pull only the IDs of the assignments into a list | |
assignment_id_list = [item.id for item in assignments] | |
# Instantiate a pool | |
pool = mp.Pool(mp.cpu_count()) | |
# Post the list to the process function, wait for the results | |
result = pool.map(process_submissions, assignment_id_list) | |
print(result) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment