import pymysql
import traceback
from multiprocessing import Pool,Manager,cpu_count
from multiprocessing.managers import BaseManager
import os,sys,time
import random # 建库建表
def createTable():
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='',
)
cur=conn.cursor()
sql_database='create database if not exists py3_userinfo default charset utf8 collate utf8_general_ci;'
sql_table='''
create table student(
id int not null auto_increment,
name varchar(20) not null,
age int default 0,
tel varchar(13) unique not null,
primary key(id)
)engine=innodb character set utf8;
'''
try:
# 建库
cur.execute(sql_database)
conn.select_db('py3_userinfo')
cur.execute('drop table if exists student;')
# 建表
cur.execute(sql_table)
except Exception as e:
print(e)
else:
print('数据库及表创建成功!')
cur.close()
conn.close() class MyMysql(object):
def __init__(self):
# 数据库连接
self.conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='',
db='py3_userinfo',
charset='utf8'
)
# 游标
self.cur = self.conn.cursor() def executeSql(self,sql):
try:
res=self.cur.execute(sql)
print('执行sql受影响的行数:',res)
self.conn.commit()
except:
traceback.print_exc() def quit(self):
self.cur.close()
self.conn.commit()
self.conn.close() class MyManager(BaseManager):
pass def my_Manager():
m=MyManager()
m.start()
return m # 把myMysql类注册到MyManager管理类中
MyManager.register('MyMysql',MyMysql) def run(my_sql):
print('subprocess is ',os.getpid())
# 造数据
name = 'ha'+str(random.randint(1,100))+'_'+str(os.getpid())
age = random.randint(1,100)
# emil=name+'@qq.com'
tel=''+str(random.choice([3,5,7,8]))+str(random.random())[2:11]
sql="insert into student(name,age,tel) values('%s','%s','%s')"%(name,age,tel)
my_sql.executeSql(sql) if __name__ == '__main__':
createTable() manager=my_Manager()
my_sql=manager.MyMysql() print('Parent process ',os.getpid())
p=Pool(cpu_count())
n=10
for i in range(n):
p.apply(run,args=(my_sql,))
p.close()
p.join()
print('all subprocess done!')
my_sql.quit()

python多进程并发插入mysql数据的更多相关文章

  1. python插入mysql数据(2)

    python插入mysql数据(2) """插入操作""" import pymysql import datetime from pymy ...

  2. Python多进程并发(multiprocessing)用法实例详解

    http://www.jb51.net/article/67116.htm 本文实例讲述了Python多进程并发(multiprocessing)用法.分享给大家供大家参考.具体分析如下: 由于Pyt ...

  3. python多进程并发和多线程并发和协程

    为什么需要并发编程? 如果程序中包含I/O操作,程序会有很高的延迟,CPU会处于等待状态,这样会浪费系统资源,浪费时间 1.Python的并发编程分为多进程并发和多线程并发 多进程并发:运行多个独立的 ...

  4. python 多进程并发与多线程并发

    本文对python支持的几种并发方式进行简单的总结. Python支持的并发分为多线程并发与多进程并发(异步IO本文不涉及).概念上来说,多进程并发即运行多个独立的程序,优势在于并发处理的任务都由操作 ...

  5. python 基础 9.3 mysql 数据操作

    #/usr/bin/python #coding=utf-8 #@Time   :2017/11/21 0:20 #@Auther :liuzhenchuan #@File   :mysql 数据操作 ...

  6. Python多进程并发操作进程池Pool

    目录: multiprocessing模块 Pool类 apply apply_async map close terminate join 进程实例 multiprocessing模块 如果你打算编 ...

  7. python多进程并发redis

    Redis支持两种持久化方式RDB和AOF,RDB持久化能够快速的储存和回复数据,但在服务器停机时会丢失大量数据,AOF持久化能够高效的提高数据的安全性,但在储存和恢复数据方面要耗费大量的时间,最好的 ...

  8. [转]Python多进程并发操作中进程池Pool的应用

    Pool类 在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量的时间.如果操作的对象数目不大时,还可以直接使用Process类动态的生成多个进程,十 ...

  9. Python多进程并发操作中进程池Pool的应用

    Pool类 在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量的时间.如果操作的对象数目不大时,还可以直接使用Process类动态的生成多个进程,十 ...

随机推荐

  1. 第06组 Alpha冲刺(5/6)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了个人中心模块的接口设计 完善后端的信息处理 GitHub签入记录 接下来的计划 ...

  2. Netty对WebSocket的支持

    WebSocket长连接 一.创建服务端代码 1.MyServer 类 public class MyServer { public static void main(String[] args) t ...

  3. APP性能测试工具GT的使用总结:app内存测试

    APP性能测试工具GT的使用总结:app内存测试 GT(随身调)是APP的随身调测平台,它是直接运行在手机上的“集成调测环境”(IDTE, Integrated Debug Environment). ...

  4. PHP系列 | ThinkPHP5数据库迁移工具 migration

    了解更多,请关注微信公众号 ThinkPHP5数据库迁移工具 migration 什么是Migration? migration用谷歌翻译是移民的意思,在PHP中我们将它理解为迁移,将Migratio ...

  5. bean名称相同冲突Annotation-specified bean name 'xx' for bean class [xxx] conflicts with existing, non-compatible bean definition of same name and class[xxx]

    工程中引入其他工程的包,由于两个工程中有重名的两个bean,导致在启动时提示如下错误: 根据bean名称在ide中查找,找到这两个重名的类,可以看到由于这两个类使用@Service标注,此时如果不使用 ...

  6. oracle远程连接服务器

    一.需要下载的工具 1.PLSQL Developer 下载及安装地址如下: http://www.zdfans.com/html/18196.html 2.下载instantclient-basic ...

  7. 如何从OA系统批量整理出邮箱地址,并导入到Foxmail 地址薄中?

    一.打开某位leader的OA,点击查看“下属” a. 将所有的下属信息 --- 全选 --- 复制 --- 粘贴到 excel 表格中 b. 分别提取“姓名” 和 “邮箱”地址信息,结合notepa ...

  8. MUD游戏开发教程视频

    MUD游戏开发教程视频 https://space.bilibili.com/476328540/channel/detail?cid=91187

  9. [LeetCode] 505. The Maze II 迷宫 II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  10. javascript获取地址栏参数的方法

    javascript获取地址栏参数的方法<pre>function GetQueryString(name){ var reg = new RegExp("(^|&)&q ...