import pymongo
import threading
import queue
client = pymongo.MongoClient(host='127.0.0.1', port=27017)
db = client['xiaoye']['jf']
class Producer(threading.Thread):
def __init__(self, data_queue, *args,**kwargs):
super(Producer, self).__init__(*args, **kwargs)
self.data = data_queue
def run(self) -> None:
try:
# 重中之重
self.data.put({'a': 'a', 'b': 'b', 'c': '积分2', 'd': '¥1'})
except:
pass
class Consumer(threading.Thread):
def __init__(self, data_queue, *args, **kwargs):
super(Consumer, self).__init__(*args, **kwargs)
self.data = data_queue
client = pymongo.MongoClient(host='127.0.0.1', port=27017)
self.db = client['xiaoye']['jf']
def run(self) -> None:
try:
# 取出队列二中的数据
print(1)
boj = self.data.get()
s = []
a = 0
for i in boj:
x = boj.get(i)
if '积分' in x:
a = 1
s.append(x)
break
if '¥' in x:
a = 2
s.append(x)
break
s.append(x)
if a == 1:
self.db.insert_one({'商品名称': s[1], '已售': s[0], '积分价': s[2]})
if a == 2:
self.db.insert_one({'商品名称': s[1], '已售': s[0], '促销价': s[2]})
except:
pass
def main():
# 把宝可梦剑盾转成16进制
# 创建图片队列(队列二)
data_queue = queue.Queue()
# 目标url 翻页的
# 开启子线程一
for x in range(50):
th = Producer(data_queue)
th.start()
# 开启子线程二
for x in range(250):
th = Consumer(data_queue)
th.start()
if __name__ == '__main__':
main()
多线程中子线程正确传输
最新推荐文章于 2023-05-16 09:27:28 发布