python多线程同步_Python多线程如何保证线程同步?

在我使用多线程的过程中,返回的结果有很多重复和缺少,我个人认为是因为线程资源不同步导致的,请问如何修改代码实现同步,和不要出现重复?是使用生产消费模型吗?

下面是代码,部分:import json

import time

import requests

from queue import Queue

from concurrent.futures import ThreadPoolExecutor, as_completed

pool = ThreadPoolExecutor(100)

def get_email():

email_queue = Queue()

with open('email.txt', 'r') as file_object:

for line in file_object:

email_queue.put(line.strip())

return email_queue

def get_proxy(retry=3):

start = 0

while start <= retry:

res = requests.get("someurl")

dic_info = res.text

dic_info = json.loads(dic_info)

status_code = dic_info['code']

if status_code != '0':

start += 1

time.sleep(0.5)

continue

data = dic_info['obj']

ip = data[0]["ip"]

port = data[0]["port"]

ip_port = 'http://{}:{}'.format(ip,port)

return {'http': ip_port}

return None

def check(email, proxy):

headers = {

'authority': 'unite.nike.com',

'pragma': 'no-cache',

'cache-control': 'no-cache',

'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',

'content-type': 'text/plain;charset=UTF-8',

'accept': '*/*',

'origin': 'https://www.baidu.com',

'sec-fetch-site': 'same-site',

'sec-fetch-mode': 'cors',

'sec-fetch-dest': 'empty',

'referer': 'https://www.baidu.com/gb/register',

'accept-language': 'zh,en;q=0.9,en-US;q=0.8,zh-CN;q=0.7'

}

params = (

('appVersion', '786'),

('experienceVersion', '786'),

('uxid', 'com.nike.commerce.nikedotcom.web'),

('locale', 'en_GB'),

('backendEnvironment', 'identity'),

('browser', 'Google Inc.'),

('os', 'undefined'),

('mobile', 'false'),

('native', 'false')

)

url = 'someurl'

data = f'{"emailAddress": "{email}"}'

try:

response = requests.post(url=url, params=params, headers=headers, data=data, proxies=proxy, timeout=8)

if response.status_code == 204:

print(f'未注册 {email}')

return f'未注册 {email}'

elif response.status_code == 409:

print(f'已注册 {email}')

return f'已注册 {email}'

else:

print(f'链接错误 {email}')

return f'链接错误 {email}'

except requests.exceptions.RequestException:

print(f'连接失败 {email}')

return f'连接失败 {email}'

def save(result):

with open('log.txt', 'a+') as file_object:

file_object.write(result + 'n')

def main():

queue = get_email()

while queue.empty != True:

email = queue.get()

proxy = get_proxy()

tasks = [pool.submit(check, email, proxy)]

for task in as_completed(tasks):

check_result = task.result()

save(check_result)

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值