繁体   English   中英

如何给每个 URL 自己的线程

[英]How to give each URL its own threading

我一直在研究一个小型 PoC,我一直在努力提高我对线程的了解,但不幸的是我被卡住了,我在这里,

import time

found_products = []

site_catalog = [
    "https://www.graffitishop.net/Sneakers",
    "https://www.graffitishop.net/T-shirts",
    "https://www.graffitishop.net/Sweatshirts",
    "https://www.graffitishop.net/Shirts"
]


def threading_feeds():
    # Create own thread for each URL as we want to run concurrent
    for get_links in site_catalog:
        monitor_feed(link=get_links)


def monitor_feed(link: str) -> None:
    old_payload = product_data(...)

    while True:
        new_payload = product_data(...)

        if old_payload != new_payload:
            for links in new_payload:
                if links not in found_products:
                    logger.info(f'Detected new link -> {found_link} | From -> {link}')
                    # Execute new thread as we don't want to block this function wait for filtering() to be done before continue
                    filtering(link=found_link)

        else:
            logger.info("Nothing new")
            time.sleep(60)
            continue


def filtering(found_link):
    ...

1 - 我目前正在尝试做一个我有多个链接开始的监视器,我的计划是我希望每个 url 同时运行,而不是需要一个一个地等待:

def threading_feeds():
   # Create own thread for each URL as we want to run concurrent
   for get_links in site_catalog:
      monitor_feed(link=get_links)

我怎样才能做到这一点?


2 - 如果我们似乎在monitor_feed内找到了给定 URL 出现的新产品,我该如何设置一个新线程来执行调用filtering(link=found_link) 我不想在它继续循环返回While True之前等待它完成,而是它应该在后台执行filtering(link=found_link) ,同时它仍然执行monitor_feed

import concurrent.futures    
with concurrent.futures.ThreadPoolExecutor() as executor:
            executor.map(monitor_feed, site_catalog)

您可以使用ThreadPoolExecutor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM