如何解决如何将单线程代码转换为多线程代码
我遇到一个问题,我将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()
解决方法
您可以使用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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。