多线程中子线程正确传输

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值