python中的ThreadPoolExecutor

#!/bin/env python3

import requests
import datetime
import threading
import csv
import json
import random
from concurrent.futures import ThreadPoolExecutor

pool = ThreadPoolExecutor(16)
threadLocal = threading.local()

reader = csv.reader(open('***.csv'))
category = ['wilson1', 'wilson2', 'wilson3']


def push(i):
    initialized = getattr(threadLocal, 'initialized', None)
    if initialized is None:
        threadLocal.initialized = True
        threadLocal.s = requests.Session()
        threadLocal.s.post(
            'http://***/***',
            json={
                'username': 'admin',
                'password': '1'
            })
    category_id = category.index(i[3])
  
    threadLocal.s.post(
        'http://***/***',
        json={
            'category_id': category_id,
            'name': i[1],
            'author': i[4],
        })


for i in reader:
    pool.submit(push, i)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章