motor的使用
# -*- coding: utf-8 -*-
# @Time : 2018/11/18 10:41 PM
# @Author : cxa
# @File : motordb.py
# @Software: PyCharm
import asyncio
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
pass
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo import UpdateOne
class MotorBase:
_db = {}
_collection = {}
def __init__(self, loop=None):
self.motor_uri = ''
self.loop = loop or asyncio.get_event_loop()
def client(self, db):
self.motor_uri = f"mongodb://localhost:27017/{db}"
return AsyncIOMotorClient(self.motor_uri, io_loop=self.loop)
def get_db(self, db='test'):
if db not in self._db:
self._db[db] = self.client(db)[db]
return self._db[db]
async def savedata():
mb = MotorBase().get_db('test')
await mb.news.insert_one({'name': "lisa"})
async def save_data(items, col="demo", key="obj_id"):
"""
:param items:
:param col:
:param key:
:return:
"""
# storage.info(f"此时的items:{items}")
# UpdateOne
mb = MotorBase().get_db("aio_spider_data")
if isinstance(items, list):
requests = list()
r_a = requests.append
for item in items:
try:
r_a(UpdateOne({
key: item.get(key)},
{'$set': item},
upsert=True))
except Exception as e:
storage.error(f"数据插入出错:{e.args}此时的item是:{item}")
await mb[col].bulk_write(requests, ordered=False, bypass_document_validation=True)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(savedata())
motor的使用的更多相关文章
- Arduino 各种模块篇 motor shield
根据arduino官方网站出的shield, 类似的情况有很多中motor shield 这里测试采用的是http://www.seeedstudio.com/wiki/Motor_Shield_V1 ...
- motor和servo
程序简单易读,不再做注释 motor.py from gpiozero import Motor from gpiozero import LED led = LED(2) motor = Motor ...
- motor helper
# -*- coding: utf-8 -*- # @Time : 2019-02-13 10:44 # @Author : cxa # @File : mongohelper.py # @Softw ...
- Tornado 中 PyMongo Motor MongoEngine 的性能测试
最近在使用 Tornado 开发 API,数据库选择了 MongoDB,因为想使用 Geo 搜索的特性.Python 可供选择的 MongoDB Drivers 可以在官网查找. 在这些 Driver ...
- AVR446_Linear speed control of stepper motor步进电机曲线分析
1.1. 单片机代码处理 // 定义定时器预分频,定时器实际时钟频率为:72MHz/(STEPMOTOR_TIMx_PRESCALER+1) #define STEPMOTOR_TIM_PRESCA ...
- Electric Motor Manufacturer - Motor Protection: 5 Questions, 5 Answers
I. Selection principle of motor protectorThe Electric Motor Manufacturer stated that the reasonab ...
- Eaton Char-Lynn Motor : Performance Of Small Displacement Motors
The small-displacement supercharged motor replaces the large-displacement motor with the speed of li ...
- Cycloid Hydraulic Motor Use: Use Failure And Treatment
The cycloidal hydraulic motor is a small low-speed, high-torque hydraulic motor with a shaft-distrib ...
- Low Speed High Torque Hydraulic Motor: Motion Performance
Crank connecting rod type low speed high torque hydraulic motor is used earlier, which is called Sta ...
随机推荐
- history新增方法
history对象包含用户访问过的URL,属于window对象的一部分,传统的使用中,它拥有length属性(浏览器历史列表URL数目) 及back().forward().go()方法. 而新的H5 ...
- 牛客练习赛 小D的剑阵 解题报告
小D的剑阵 题意链接: https://ac.nowcoder.com/acm/contest/369/F 来源:牛客网 现在你有 \(n\) 把灵剑,其中选择第i把灵剑会得到的 \(w_i\) 攻击 ...
- JAVA8给我带了什么——lambda表达
这此年来我一直从事.NET的开发.对于JAVA我内心深处还是很向往的.当然这并不是说我不喜欢.NET.只是觉得JAVA也许才是笔者最后的归处.MK公司是以.NET起家的.而笔者也因为兄弟的原因转行.N ...
- A1033. To Fill or Not to Fill
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...
- 【CH5302】金字塔 区间DP
题目大意:给定一棵树,树上点有标记,给定一棵树的\(dfs\)序标记序列,求有多少种可能的子树形态.(子树之间有序) 这是一道区间计数类DP,涉及到树的\(dfs\)序. 这道题区间的划分点 \(k\ ...
- Java逐行写入字符串到文件
下边是写东西到一个文件中的Java代码.运行后每一次,一个新的文件被创建,并且之前一个也将会被新的文件替代.这和给文件追加内容是不同的. 1. public static void writeFile ...
- NO.9: 令operator=返回一个reference to *this
1.令赋值操作返回一个reference to *this(除非你有个标新立异的理由,那就随大众- - )
- poj 3207(2-SAT+SCC)
传送门:Problem 3207 https://www.cnblogs.com/violet-acmer/p/9769406.html 难点: 题意理解. 题意: 平面上有一个圆,圆上有n个点(分别 ...
- poj 1236(强连通分量分解模板题)
传送门 题意: N(2<N<100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输. 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都 ...
- Linux Shell 笔记
1.查看进程的环境变量 普通:$cat /proc/1642/environ 换行:$cat /proc/1642/environ | tr '\0' '\n' tr的命令格式是tr SET1 SE ...