#!/usr/bin/env python # -*- coding: utf-8 -*- import os,time,random,json from shutil import copyfile,move from multiprocessing import Process,Queue from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from core.config import settings from utils.fileuploader import ossfile_uploader from models.paper import * from models.mark import * engine = create_engine(settings.SYNC_MYSQL_URI) session = sessionmaker(bind=engine) def get_marktask(task_id): """ """ qset = session().query(MarkTask).filter(MarkTask.id==task_id) if qset: task = qset.one() return task return None def update_student_marktask(task_id,sno,imgs): """ """ dbsession = session() task = dbsession.query(StudentMarkTask).filter(StudentMarkTask.task_id==task_id)\ .filter(StudentMarkTask.student_sno==sno).one() task.pimgs = json.dumps(imgs) dbsession.commit() dbsession.close() def produce_papers(q): """ """ while True: if not os.path.exists(settings.PAPERS_PATH): os.makedirs(settings.PAPERS_PATH) for pimg in os.listdir(settings.PAPERS_PATH): if pimg.startswith("T"): pfile = os.path.join(settings.PAPERS_PATH,pimg) if os.path.isdir(pfile): for ppimg in os.listdir(pfile): src = os.path.join(pfile,ppimg) if os.path.isfile(src): print(f"produce img:{src}") workspace = os.path.join(pfile,"workspace") if not os.path.exists(workspace): os.makedirs(workspace) target = os.path.join(workspace,ppimg) q.put(target) move(src,target) time.sleep(0.1) def rec_papers(simgs,task_id): """ """ sno = "" urls = [] for img in simgs: print(f"start rec img:{img}") sno = random.choice(["202100010","202100011"]) pieces = [{ "sno":sno, "qno":1, "sqno":0, "qimg":"http://osscache.scxjc.club/static/say365/ques/XZBGZSXRABX1U1L1_1_1_0.png" }] ossfile = img.replace(settings.PAPERS_PATH,"").lstrip("/") content_type = {"Content-Type":"image/png"} url = ossfile_uploader.resumable_upload_from_local(img,ossfile,content_type) print(url) urls.append(url) return sno,urls,pieces def consumer_papers(q): """ """ while True: pimg = q.get() if pimg: pfile = os.path.join(settings.PAPERS_PATH,pimg) task_id = pfile.split("workspace")[0].split("-")[0].split("/")[-1].lstrip("T") task = get_marktask(task_id) if task: pages = task.pages simgs = [] for i in range(pages): simgs.append(pfile) sno,imgs,pieces = rec_papers(simgs,task_id) update_student_marktask(task_id,sno,imgs) print(f"consumer and remove img:{pfile}") time.sleep(0.1) if __name__ == "__main__": q = Queue() p1 = Process(target=produce_papers,args=(q,)) p2 = Process(target=consumer_papers,args=(q,)) p1.start() p2.start()