pymysql redis 多线程 多进程

 1.pymysql

import pymysql

conn = pymysql.connect(host='192.168.20.111',
                                port=3306,
                                user='root',
                                password='123456',
                                db='mydb',
                                charset='utf8',)

cur = conn.cursor()

###########增改删##############
sqls = "insert into *****"
cur.execute(sqls)
conn.commit()
cur.close()

###############查##############
sqls = "insert into *****"
cur.execute(sqls)
data = cur.fetchall()
cur.close()

###############撤回操作##########
try:
    #数据库操作1
    #数据库操作2
    #....
except:
    conn.rollback()
else:
    conn.commit()

###########获取刚插入数据的id############
sqls = "insert into *****"
cur.execute(sqls)
conn.commit()
id = conn.insert_id()

 

2.多进程共享数据

from multiprocessing import Process
from multiprocessing import Manager  #进程数据管理
 
 
 
def f1(i,dic):
    dic[i] = 200+i
    print(dic.values())
 
if __name__ =='__main__':#进程间默认不能共用内存
    manager = Manager()
    dic = manager.dict()#这是一个特殊的字典(manager创建字典)
 
 
    for i in range(10):
        p = Process(target=f1,args=(i,dic))  #进程共享字典的内存
        p.start()
        p.join()   #回收进程

3.redis

import redis
r = redis.Redis(host='192.168.32.111', port=6379, db=2)
data_get = r.lrange('weibo:info', 0, -1) #取数据,redis仍然保存数据,rrange
data_pop = r.lpop('weibo:info')  #冒泡取数据,rpop

info_data = "********"
r.lpush('weibo:info', info_data)  #写入数据,rpush

 

你可能感兴趣的:(工具教程)