Python多线程
#!/usr/bin/python3 import pymysql import os import datetime import threading import time import requests rangetime = 100 start = datetime.datetime.now() os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print ("开始线程:" + self.name) print_time(self.name, self.counter) print ("退出线程:" + self.name) def print_time(threadName, counter): while counter: # 执行业务 url = "https://chp.shadiao.app/api.php" time.sleep(1) r = download_page(url) list = [] data = [r] list.append(data) result = bathInsertChpData(list) print(threadName, ' ** ', rangetime - counter , ' ** ',result) counter -= 1 def main(): t_list=[] for i in range(80): #启动线程数 t = myThread(i, "Thread-%s"%i, rangetime) t_list.append(t) t.start() for t in t_list: t.join() print("退出主线程") if __name__ == '__main__': main()