paper_consumer.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os,time,random,json
  4. from shutil import copyfile,move
  5. from multiprocessing import Process,Queue
  6. from sqlalchemy import create_engine
  7. from sqlalchemy.orm import sessionmaker
  8. from core.config import settings
  9. from utils.fileuploader import ossfile_uploader
  10. from models.paper import *
  11. from models.mark import *
  12. engine = create_engine(settings.SYNC_MYSQL_URI)
  13. session = sessionmaker(bind=engine)
  14. def get_marktask(task_id):
  15. """
  16. """
  17. qset = session().query(MarkTask).filter(MarkTask.id==task_id)
  18. if qset:
  19. task = qset.one()
  20. return task
  21. return None
  22. def update_student_marktask(task_id,sno,imgs):
  23. """
  24. """
  25. dbsession = session()
  26. task = dbsession.query(StudentMarkTask).filter(StudentMarkTask.task_id==task_id)\
  27. .filter(StudentMarkTask.student_sno==sno).one()
  28. task.pimgs = json.dumps(imgs)
  29. dbsession.commit()
  30. dbsession.close()
  31. def produce_papers(q):
  32. """
  33. """
  34. while True:
  35. if not os.path.exists(settings.PAPERS_PATH):
  36. os.makedirs(settings.PAPERS_PATH)
  37. for pimg in os.listdir(settings.PAPERS_PATH):
  38. if pimg.startswith("T"):
  39. pfile = os.path.join(settings.PAPERS_PATH,pimg)
  40. if os.path.isdir(pfile):
  41. for ppimg in os.listdir(pfile):
  42. src = os.path.join(pfile,ppimg)
  43. if os.path.isfile(src):
  44. print(f"produce img:{src}")
  45. workspace = os.path.join(pfile,"workspace")
  46. if not os.path.exists(workspace):
  47. os.makedirs(workspace)
  48. target = os.path.join(workspace,ppimg)
  49. q.put(target)
  50. move(src,target)
  51. time.sleep(0.1)
  52. def rec_papers(simgs,task_id):
  53. """
  54. """
  55. sno = ""
  56. urls = []
  57. for img in simgs:
  58. print(f"start rec img:{img}")
  59. sno = random.choice(["202100010","202100011"])
  60. pieces = [{
  61. "sno":sno,
  62. "qno":1,
  63. "sqno":0,
  64. "qimg":"http://osscache.scxjc.club/static/say365/ques/XZBGZSXRABX1U1L1_1_1_0.png"
  65. }]
  66. ossfile = img.replace(settings.PAPERS_PATH,"").lstrip("/")
  67. content_type = {"Content-Type":"image/png"}
  68. url = ossfile_uploader.resumable_upload_from_local(img,ossfile,content_type)
  69. print(url)
  70. urls.append(url)
  71. return sno,urls,pieces
  72. def consumer_papers(q):
  73. """
  74. """
  75. while True:
  76. pimg = q.get()
  77. if pimg:
  78. pfile = os.path.join(settings.PAPERS_PATH,pimg)
  79. task_id = pfile.split("workspace")[0].split("-")[0].split("/")[-1].lstrip("T")
  80. task = get_marktask(task_id)
  81. if task:
  82. pages = task.pages
  83. simgs = []
  84. for i in range(pages):
  85. simgs.append(pfile)
  86. sno,imgs,pieces = rec_papers(simgs,task_id)
  87. update_student_marktask(task_id,sno,imgs)
  88. print(f"consumer and remove img:{pfile}")
  89. time.sleep(0.1)
  90. if __name__ == "__main__":
  91. q = Queue()
  92. p1 = Process(target=produce_papers,args=(q,))
  93. p2 = Process(target=consumer_papers,args=(q,))
  94. p1.start()
  95. p2.start()