在线程python前面打印任务编号?

在线程python前面打印任务编号?,python,Python,我目前有一个脚本,它可以刮取CSV,然后对CSV中的n行运行函数n次,请参见代码: def link_check(product_link): response1 = requests.get(product_link, headers=headers) html1 = response1.content if something not in html1: print(row['Link'], "is an invalid link, check you

我目前有一个脚本,它可以刮取CSV,然后对CSV中的n行运行函数n次,请参见代码:

def link_check(product_link):
    response1 = requests.get(product_link, headers=headers)
    html1 = response1.content
    if something not in html1:
        print(row['Link'], "is an invalid link, check your link")
    else:
        function(product_link, product_name)



def function(product_link, product_name):
    response1 = requests.get(product_link, headers=headers)
    html1 = response1.content
    print(datetime.datetime.now(), "Monitoring", product_name)
    print(datetime.datetime.now(),
          'First HTML scraped successfully, sleeping')
# More code here which is not relevant



with open("tasks.csv", newline='') as file:
    reader = csv.DictReader(file)
    for row in reader:
        if row['Name'] == "x":
            threading.Thread(target=link_check, args=(row['Link'],)).start()
        else:
            print("not available at this time")
我想知道是否可以在函数中打印线程号,或者在表单中打印行号

[Task 1]-13:00:00:00-第一个HTML已成功删除


其中,任务编号是CSV中的行号(不包括标题行)。

您可以将递增计数器的值作为参数传递给辅助函数:

from itertools import count

def link_check(product_link, task_number):
    response1 = requests.get(product_link, headers=headers)
    html1 = response1.content
    if something not in html1:
        print(row['Link'], "is an invalid link, check your link")
    else:
        function(product_link, product_name, task_number)

def function(product_link, product_name, task_number):
    response1 = requests.get(product_link, headers=headers)
    html1 = response1.content
    print(datetime.datetime.now(), "Monitoring", product_name)
    print(f'[Task {task_number}]', datetime.datetime.now(),
          'First HTML scraped successfully, sleeping', sep=' - ')

counter = count(1)
with open("tasks.csv", newline='') as file:
    reader = csv.DictReader(file)
    for row in reader:
        if row['Name'] == "x":
            threading.Thread(target=link_check, args=(row['Link'], next(counter))).start()
        else:
            print("not available at this time")

可以将递增计数器的值作为参数传递给辅助函数:

from itertools import count

def link_check(product_link, task_number):
    response1 = requests.get(product_link, headers=headers)
    html1 = response1.content
    if something not in html1:
        print(row['Link'], "is an invalid link, check your link")
    else:
        function(product_link, product_name, task_number)

def function(product_link, product_name, task_number):
    response1 = requests.get(product_link, headers=headers)
    html1 = response1.content
    print(datetime.datetime.now(), "Monitoring", product_name)
    print(f'[Task {task_number}]', datetime.datetime.now(),
          'First HTML scraped successfully, sleeping', sep=' - ')

counter = count(1)
with open("tasks.csv", newline='') as file:
    reader = csv.DictReader(file)
    for row in reader:
        if row['Name'] == "x":
            threading.Thread(target=link_check, args=(row['Link'], next(counter))).start()
        else:
            print("not available at this time")
我想知道是否有可能在 作用

是的,您可以在当前线程执行函数时获取其id。发件人:

返回当前线程的“线程标识符”。这是一个非零整数。其价值没有直接意义;它是一个神奇的cookie,用于索引特定于线程的数据字典。当一个线程退出并创建另一个线程时,可以回收线程标识符

您还可以通过
threading.current_thread().ident获取id

我想知道是否有可能在 作用

是的,您可以在当前线程执行函数时获取其id。发件人:

返回当前线程的“线程标识符”。这是一个非零整数。其价值没有直接意义;它是一个神奇的cookie,用于索引特定于线程的数据字典。当一个线程退出并创建另一个线程时,可以回收线程标识符

您还可以通过
threading.current_thread().ident获取id