码迷,mamicode.com
首页 > 系统相关 > 详细

进程实例

时间:2018-12-14 19:28:38      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:span   err   val   ctime   测试数据   XML   date   obj   lease   

import pymysql
import urllib3
from lxml import etree
import logging
import requests
import queue
import time
import threading
from threading import RLock
import re

thread_num = 0
lock = RLock()
import multiprocessing

# 添加日志

logging.basicConfig(
    level=logging.INFO,  # 定义输出到文件的log级别,大于此级别的都被输出
    format=%(asctime)s  %(filename)s  %(levelname)s : %(message)s,  # 定义输出log的格式
    datefmt=%Y-%m-%d %H:%M:%S,  # 时间
    filename=druginfoError.log,  # log文件名
    filemode=a)  # 写入模式“w”或“a”


class yaoyuan(object):

    def __init__(self):
        self.strat_record = 1
        self.end_record = 10000001
        # self.db = pymysql.connect(host=‘localhost‘, port=3306, database=‘druginfo‘, user=‘root‘, password=‘mysql‘, charset=‘utf8‘)
        self.db = pymysql.connect(host=rm-bp195i4u0w1066u709o.mysql.rds.aliyuncs.com, port=3306, database=druginfo,
                                  user=qygwroot, password=kangcenet@123, charset=utf8)
        self.cursor = self.db.cursor()
        self.parse_page()

    def parse_page(self):
        star_time = time.time()
        threading_list = []
        # pass
        # print(‘这是一行mysql的测试数据‘)

        mypool = multiprocessing.Pool(10)
        # 并发10条进程
        for i in range(1000):
            # self.parse_page_data(i)
            mypool.apply_async(self.parse_page_data, (i,))
        # print(1)
        # 关闭进程池
        mypool.close()
        # 阻塞等待
        mypool.join()

        times = time.time() - star_time
        print(times)

    def parse_page_data(self, a):
        print(a)
        # self.db = pymysql.connect(host=‘localhost‘, port=3306, database=‘druginfo‘, user=‘root‘, password=‘mysql‘, charset=‘utf8‘)
        db = pymysql.connect(host=rm-bp195i4u0w1066u709o.mysql.rds.aliyuncs.com, port=3306, database=druginfo,
                             user=qygwroot, password=kangcenet@123, charset=utf8)
        cursor = db.cursor()
        drugsql = "insert into text(name) values(‘{}‘)"
        drugsql_data = drugsql.format(这是一行mysql的测试数据)
        # lock.acquire()
        cursor.execute(drugsql_data)
        db.commit()
        # lock.release()


if __name__ == __main__:
    a = yaoyuan()

 

进程实例

标签:span   err   val   ctime   测试数据   XML   date   obj   lease   

原文地址:https://www.cnblogs.com/yoyo1216/p/10120476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!