pymongo的多线程

时间:2018-07-09 15:08:21

标签: python multithreading pymongo

我对如何使用pymongo创建线程有疑问。我有下一个代码,它的行为等同于一个秘密执行:

import threading
from pymongo import MongoClient

def scrap(gte, lt):
    """funcion que realiza el trabajo en el thread"""
    print(threading.current_thread().getName())
    client = MongoClient('localhost', 27017)
    db = client.api_lantia
    lista = list(db.prueba_portales.find({'_id':{'$gte': gte, '$lt' : lt}}))
    for page in lista:
        print(str(page['_id']) + ' ' + page['portal'])
    return


t1 = threading.Thread(scrap(0,20))
t2 = threading.Thread(scrap(20,40))
t3 = threading.Thread(scrap(40,60))
t4 = threading.Thread(scrap(60,80))
t5 = threading.Thread(scrap(80,105))
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()

我需要在python中创建多线程,并且它们在“同一时间”启动。

谢谢!

0 个答案:

没有答案