为了账号安全,请及时绑定邮箱和手机立即绑定

如何在多线程中执行单个函数并在python中循环创建线程实例?

如何在多线程中执行单个函数并在python中循环创建线程实例?

心有法竹 2022-06-28 10:03:23
1)我有一个产品链接列表,它包含 3385 个链接2)我有一个函数 get_pro_info(link) 它获取产品链接并将项目附加到 json 文件。3)我希望 selenium 打开 5 个浏览器和 5 个并行链接并获取产品信息并附加到文件或列表中。或 3)硒打开 1 个浏览器和 5 个选项卡(有 5 个链接)并附加文件。问题我如何在我的代码上应用线程?我的代码...new_url=''def get_pro_info(pro_url):    driver = webdriver.Chrome(executable_path=r'C:\Users\Beenu\PycharmProjects/chromedriver.exe')    try:        new_url = 'https://pk.studiobytcs.com' + pro_url        print('new product URL: ' + new_url)        driver.execute_script("window.open('');")        sleep(1)        # use to switch control        driver.switch_to.window(driver.window_handles[0])        # sleep(1)        driver.get(new_url)    except(WebDriverException, selenium.common.exceptions.TimeoutException, Exception) as e:        print('There is error in getting Product by URL in get_pro_info()! \n' + str(e.stacktrace))        pass    description_source_code = ''    # description_soup = BeautifulSoup()    description_soup: BeautifulSoup = object    # global description_soup    try:        # description_soup = BeautifulSoup('html.parser')        description: WebElement = driver.find_element_by_xpath(            '//*[@id="shopify-section-product-template"]/div[2]/div[1]/div/div[2]')        description_source_code = description.get_attribute("innerHTML")        description_soup: BeautifulSoup = BeautifulSoup(description_source_code, 'html.parser')    except NoSuchElementException as e:        print('Product description taag not found! \n' + str(e.stacktrace))        pass    # 179 here    # This is for getting heading product name
查看完整描述

1 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

如果您想迭代列表并始终获得 20 个链接,那么您可以使用range(start, stop, step)withstep=20


all_t = []


for i in range(0, len(list_of_product_link), 20):

     twenty_links = list_of_product_link[i:i+20]


     t = threading.Thread(target=get_product_info, args=(twenty_links,))

     t.start()

     all_t.append(t)


# --- later ---


for t in all_t:

     t.join()

或者


for i in range(0, len(list_of_product_link), 20):

     twenty_links = list_of_product_link[i:i+20]


     all_t = []


     for link in twenty_links:    

          t = threading.Thread(target=get_product_info, args=(link,))

          t.start()

          all_t.append(t)


     # --- inside first `for` loop ---


     for t in all_t:

         t.join()

如果您以后不需要您的清单,其他方法很好


all_t = []


while list_of_product_link:

     twenty_links = list_of_product_link[:20]

     list_of_product_link = list_of_product_link[20:]


     t = threading.Thread(target=get_product_info, args=(twenty_links,))

     t.start()

     all_t.append(t)


# --- later ---


for t in all_t:

     t.join()

或者


while list_of_product_link:

     twenty_links = list_of_product_link[:20]

     list_of_product_link = list_of_product_link[20:]


      all_t = []


     for link in twenty_links:    

          t = threading.Thread(target=get_product_info, args=(link,))

          t.start()

          all_t.append(t)


     # --- inside first `for` loop ---


     for t in all_t:

         t.join()

顺便说一句:args=需要元组——即使你只有一个参数,所以你需要,用( )一个元素创建元组。


顺便说一句:如果您希望它每时每刻只运行 20 个线程,那么最好查看多处理和Pool(20)


 from multiprocessing import Pool


def get_product_info(link):

    result = ....

    return result


if __name__ == '__main__':

    with Pool(20) as p:

        all_results = p.map(get_product_info, list_of_product_link)


查看完整回答
反对 回复 2022-06-28
  • 1 回答
  • 0 关注
  • 186 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号