def main():
    urls = [
        'https://talkpython.fm',
        'https://pythonbytes.fm',
        'https://google.com',
        'https://realpython.com',
        'https://training.talkpython.fm/',
    ]

    work = []

    with PoolExecutor() as executor:
        for url in urls:
            # print("Getting title from {}".format(url.replace('https', '')),
            #       end='... ',
            #       flush=True)
            # title = get_title(url)
            f: Future = executor.submit(get_title, url, time.time())
            work.append(f)

        print("Waiting for downloads...", flush=True)

    print("All jobs submitted", flush=True)
    for f in work:
        print("{}".format(f.result()), flush=True)
Пример #2
0
def main():
    work = []
    for i in range(0, 5):
        with PoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
            t0 = datetime.datetime.now()
            m: cython.int = np.random.randint(0, 5)
            n: cython.int = np.random.randint(0, 5)
            f = executor.submit(ackerman.ack, m, n)
            work.append(f)
            t1 = datetime.datetime.now()
            final_time = t1 - t0

        print("Ackerman of {} and {} is: {} in {:,.5f}".format(
            m, n, f.result(), final_time.total_seconds()))
Пример #3
0
    def post(self):
        t0 = datetime.datetime.now()
        film_urls = self.get_films_urls()
        work = []
        with PoolExecutor() as executor:
            for film_url in film_urls:
                f = executor.submit(self.parse_film, film_url)
                work.append(f)
        films_to_create = [f.result() for f in work]
        created_films = self.populate_db_with_films(films_to_create)
        dt = datetime.datetime.now() - t0
        print(f'Populating time :{dt.total_seconds():.2f} sec.')

        return {'Message' : f'Database populated with {created_films} films'}, 201
def threaded_add_all_vals_to_sheet_dict(wb_name: str,
                                        sheet_dict: SheetDict) -> SheetDict:
    header_tasks = []
    with PoolExecutor() as executor:
        for ws_name in sheet_dict:
            f: Future = \
                executor.submit(prepare_sheet_headers_fetch, wb_name, ws_name, sheet_dict)
            header_tasks.append(f)

    # Collect results from complete tasks
    for task in header_tasks:
        ws_name, headers, vals = task.result()
        sheet_dict[ws_name]['headers'] = headers
        sheet_dict[ws_name]['vals'] = vals

    return wb_name, sheet_dict
Пример #5
0
def threaded_open_workbooks(wbs_names: List[str]) -> Dict[str, Spreadsheet]:
    print(f"Opening {len(wbs_names)} workbooks...")
    wbs_open_tasks = []
    with PoolExecutor() as executor:
        for wb_name in wbs_names:
            f: Future = executor.submit(open_workbook, wb_name)
            wbs_open_tasks.append(f)

    wbs_open = {}
    for i, task in enumerate(wbs_open_tasks):
        wb_name, open_wb = task.result()
        print(f"{i+1}. Fetched '{wb_name}'")
        wbs_open[wb_name] = open_wb

    print(f"\nOpened {len(wbs_open)} workbooks from Google Sheets\n")

    return wbs_open
def main():
    urls = [
        'https://talkpython.fm',
        'https://pythonbytes.fm',
        'https://google.com',
        'https://realpython.com',
    ]

    work = []
    with PoolExecutor() as executor:
        for url in urls:
            f: Future = executor.submit(get_title, url)
            work.append(f)

        print('Waiting for downloads...', flush=True)

    print('Done', flush=True)

    for f in work:
        print('{}'.format(f.result()), flush=True)
def threaded_add_all_vals_to_sheet_dicts(
        sheet_dicts: SheetDicts) -> SheetDicts:
    print("\n=======================================")
    print(f"Fetching Sheet Headers for {len(sheet_dicts)} workbooks")
    print("=======================================")

    sheet_dicts_update_tasks = []
    with PoolExecutor() as executor:
        for wb_name, sheet_dict in sheet_dicts.items():
            f: Future = executor.submit(threaded_add_all_vals_to_sheet_dict,
                                        wb_name, sheet_dict)
            sheet_dicts_update_tasks.append(f)

    # Collect results from complete tasks
    updated_sheet_dicts = {}
    for task in sheet_dicts_update_tasks:
        wb_name, sheet_dict = task.result()
        updated_sheet_dicts[wb_name] = sheet_dict

    return updated_sheet_dicts
def threaded_wbs_to_sheet_dicts(
        wbs_open: Dict[str, Spreadsheet]) -> SheetDicts:
    print(f"\n======================")
    print(f"Collecting Sheet Dicts")
    print(f"======================\n")

    sheet_dicts_tasks = []
    with PoolExecutor() as executor:
        for wb_name, wb in wbs_open.items():
            f: Future = executor.submit(build_sheet_dict, wb_name, wb)
            sheet_dicts_tasks.append(f)

    # Collect results from complete tasks
    sheet_dicts = {
        sheet_dict.result()[0]: sheet_dict.result()[1]
        for sheet_dict in sheet_dicts_tasks
    }
    print(
        f"\nFinished collecting sheet_dicts: {len(sheet_dicts)} sheet_dicts collected\n"
    )

    return sheet_dicts