多线程 python 问题,无法打印

Multi Threading python problem, not printing

我目前正在学习多线程并学习了 concurrent.futures 和线程池执行器,我试图实现一个示例但由于某种原因它没有打印多个打印命令。我哪里错了?

import requests
import random
import string
import concurrent.futures

result = open(r"workingGhosts.txt","w")
length = 5
url ="https://ghostbin.co/paste/"

def get_random_string(length):
    letters = string.ascii_lowercase
    result_str = ''.join(random.choice(letters) for i in range(length))
    return result_str

times = int(input("how many times do you want to check?"))

list_urls=[]

counter = 0
for x in range(times):
    stringA = url + get_random_string(length)
    list_urls.append(stringA)

def lol(finalUrl):
    r = requests.get(finalUrl)
    counter= counter +1
    print("printing")
    if r.status_code != 404:
        result.write(finalUrl)


with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(lol,list_urls)

我没有打印,因为有一个例外。这些不会显示给你,除非你在使用执行程序时自己检查它们
您得到的异常是:

UnboundLocalError: local variable 'counter' referenced before assignment

因为这条线

counter= counter +1

像这样更改 lol

def lol(finalUrl):
    global counter  # add this line
    r = requests.get(finalUrl)
    counter= counter +1
    print("printing")
    if r.status_code != 404:
        result.write(finalUrl)