简体   繁体   English

如何给每个 URL 自己的线程

[英]How to give each URL its own threading

I have been working on a small PoC where I have been trying to improve my knowledge with Threading but unfortunately I got stuck and here I am,我一直在研究一个小型 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 - Im currently trying to do a monitor where I have multiple links to start with, my plan is that I would like to each url to run concurrently instead of needing to wait 1 by 1: 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)

How can I do that?我怎样才能做到这一点?


2 - If we seem to find a new product that has appeared to the given URL inside the monitor_feed , how can I set a new thread to execute the call filtering(link=found_link) ? 2 - 如果我们似乎在monitor_feed内找到了给定 URL 出现的新产品,我该如何设置一个新线程来执行调用filtering(link=found_link) I dont want to wait for it to be done before it continues to loop back for the While True but instead it should do the filtering(link=found_link) in the background while it stil executes the monitor_feed我不想在它继续循环返回While True之前等待它完成,而是它应该在后台执行filtering(link=found_link) ,同时它仍然执行monitor_feed

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

You can use ThreadPoolExecutor .您可以使用ThreadPoolExecutor

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

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