python motor异步mongodb操作

from motor.motor_asyncio import AsyncIOMotorClient
from asyncio.coroutines import coroutine
import datetime
import asyncio
import time
from multiprocessing import Process
import random




__author__ = "Ennis"




@coroutine
def do_find(vin):
    print("start to find document: " + vin)
    start_time = time.time()
    db = AsyncIOMotorClient('mongodb://192.168.1.212:12345')["scooter"]
    count = 0
    cursor = db['tVehiclePosition'].find({'vin': vin, 'reporttime':
        {'$lt': datetime.datetime(2017, 4, 30, 11, 11, 11), '$gt': datetime.datetime(2017, 4, 1, 11, 11, 11)}})
    while (yield from cursor.fetch_next):
        count += 1
        cursor.next_object()
    end_time = time.time()
    print("elapsed time:", count, end_time - start_time)




def start_fetch_process(phone):
    loop = asyncio.get_event_loop()
    loop.run_until_complete(do_find(phone))


if __name__ == "__main__":
    processes = []
    for index in range(50):
        process = Process(target=start_fetch_process, args=(str(1000000000 + random.randint(0, 299999)), ))
        processes.append(process)
        process.start()


        for process in processes:
            process.join()

你可能感兴趣的:(python)