Python3多线程并发写入数据到MongoDB
2023-08-22 14:34 ndzj 阅读(1) 评论(0) 编辑 收藏 举报!/usr/bin/python
! * coding:utf-8 *
from pymongo import MongoClient
import threading
import time
连接单机
single mongo
c = MongoClient(host="192.168.89.151", port=27017)
连接集群
def worker():
"""线程执行的函数"""
c = MongoClient('mongodb://172.xx.xx.204:30000,172.xx.xx.205:30000,172.xx.xx.206:30000')
db = c.eoo
myset = db.bar
for i in range(1,5000):
myset.insert({"u" :str(i), "name" : "sz"+str(i), "city" : "nn"+str(i)})
print("线程启动")
threads = []
for i in range(100):
t = threading.Thread(target=worker)
threads.append(t)
t.start()
for t in threads:
t.join()