代码之家  ›  专栏  ›  技术社区  ›  Jason Jurotich

如何在Python中从ThreadPoolExecutor进程返回列表

  •  0
  • Jason Jurotich  · 技术社区  · 4 年前

    我试图从Python中的ThreadPoolExecutor进程返回一个列表,但在这种情况下,该列表“有效”,在运行完毕后仍为空。

    其余的代码都能正常工作(主要是因为,当我尝试从太多的类中提取时,它有时会偶尔抛出“malloc:不正确的校验和”或“分段错误”错误)。

    无论哪种方式,我都需要能够将每个循环的所有结果放入一个列表中,并在主函数末尾返回。

    我根据注释修改了代码,只是为了让它尽可能简单。

    from googleapiclient.discovery import build
    from google.oauth2 import service_account as sa
    from datetime import datetime,timedelta
    import time
    import concurrent.futures as cf
    
    key = 'test.json'
    email = 'jj@school.com'
    
    works = []
    
    scopes = [
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/classroom.courses",
        "https://www.googleapis.com/auth/classroom.coursework.me",
        "https://www.googleapis.com/auth/classroom.coursework.students",
    ]
    
    cr = sa.Credentials.from_service_account_file(
      key, scopes=scopes).with_subject(email)
    
    sh = build('sheets', 'v4', credentials=cr).spreadsheets()
    cl = build('classroom', 'v1', credentials=cr)
    
    
    def listClasses():
      semdate = "2020/08/01"
      cr1 = datetime.strptime(semdate, '%Y/%m/%d').date()
      pr1 = "profe@school.edu.mx"
      arr1 = []
    
      re1 = cl.courses().list(
        teacherId=pr1,
        courseStates="ACTIVE"
        ).execute()
      courses = re1.get('courses', [])
    
      for i in courses:
        ct = i['creationTime'][:10].replace("-","/")
        cr2 = datetime.strptime(ct,'%Y/%m/%d').date()
        ci = i['id']
        if cr2 > cr1:
          arr1.append(ci)
    
      return arr1
    
    
    def lcw(i):
        res1= cl.courses().courseWork().list(courseId=i).execute()
        works = res1.get('courseWork', [])
        if len(works) != 0:
          return works 
    
    
    def main():
      lcs = listClasses()  
    
      start = time.perf_counter()
    
      with cf.ThreadPoolExecutor() as excut:
        x = excut.map(lcw,lcs)
        y = [i.result() for i in x]
        print(y)
    
      fin = time.perf_counter()
      print(f"Done in {round(fin-start, 2)} seconds")
     
    
    if __name__ == '__main__':
       main()
    
    0 回复  |  直到 4 年前