如何将单线程代码转换为多线程代码

时间:2020-09-30 11:19:56

标签: python-3.x python-multithreading

我遇到一个问题,我将currentPlace字符串声明为全局字符串,但是我认为一旦转换为str后是否正确,它将覆盖正确的全局设置吗?我对此一无所知。任何帮助将不胜感激,谢谢。

代码:

    from proxy_checker import ProxyChecker
    import json
    import threading
    
    
    
    
    # define an empty list
    places = []
    threads = []
    
    def check():
        global check
        # open file and read the content in a list
        with open('prox_list.txt', 'r') as filehandle:
            for line in filehandle:
            # remove linebreak which is the last character of the string
                global currentPlace
                currentPlace = line[:-1]
            # add item to the list
                places.append(currentPlace)
                checker = ProxyChecker()
                output = checker.check_proxy(str(currentPlace))
                print(str(currentPlace) + " " + str(output))
    
    
        with open('output_prox.txt', 'w') as filehandle:
            json.dump(currentPlace, filehandle)
    
    
    
    for i,link in enumerate(str(currentPlace)):
        t = threading.Thread(target=check, args=(i, link))
        t.start()
        threads.append(t)
    
    for thread in threads:
        thread.join()

1 个答案:

答案 0 :(得分:0)

您可以使用multiprocessing.dummy.Pool class来轻松实现多线程接口。 (之所以称为“虚拟”,是因为它不是真正的多处理程序,它仅存在于同一模块中。)

编写您的辅助函数,使其接受参数并返回其值(就像编写其他函数一样),并完全避免使用全局变量。使用Pool#map method在输入值列表上映射辅助函数:

from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import cpu_count
from proxy_checker import ProxyChecker

def check(place):
    checker = ProxyChecker()
    output = checker.check_proxy(place)
    return place, output

places = []
with open('prox_list.txt', encoding='utf8') as filehandle:
    for line in filehandle:
        places.append(line[:-1])

pool = ThreadPool(cpu_count())

results = pool.map(check, places)
print(results)
相关问题