python mongoclient_Python-如何使用MongoDB(pymongo)&不使用“MongoClient opened befork”的多进程。问题?...

注意警告

get_mongo_client()

;如果你想从任何地方打到安全的电话,你需要“标记”

_mongo_client

使用当前进程的PID,如果对象有错误的PID,则丢弃该对象。

_mongo_client = None # Global per process

def get_mongo_client():

# Make sure not to call this within the master process, or things

# will break again.

global _mongo_client

if _mongo_client is None:

_mongo_client = pymongo.MongoClient("mongodb://localhost:27017/")

return _mongo_client

def get_mongo_col(collection, database="testdb"):

client = get_mongo_client()

return client[database][collection]

def func1():

col1 = get_mongo_col("col1")

while True:

col1.insert_one({})

# ...

def func2():

col2 = get_mongo_col("col2")

while True:

col2.insert_one({})

# ...

def main():

# Multiproccesing

p1 = Process(target=func1)

p2 = Process(target=func2)

p1.start()

p2.start()

p1.join()

p2.join()

if __name__ == "__main__":

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值