Python的ThreadPoolExecutor如何高效管理线程?
- 内容介绍
- 文章标签
- 相关推荐
本文共计157个文字,预计阅读时间需要1分钟。
当然可以,请提供需要改写的原文,我将为您进行修改。
#!/bin/env python3import 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(
'***/***',
json={
'username': 'admin',
'password': '1'
})
category_id = category.index(i[3])
threadLocal.s.post(
'***/***',
json={
'category_id': category_id,
'name': i[1],
'author': i[4],
})
for i in reader:
pool.submit(push, i)
本文共计157个文字,预计阅读时间需要1分钟。
当然可以,请提供需要改写的原文,我将为您进行修改。
#!/bin/env python3import 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(
'***/***',
json={
'username': 'admin',
'password': '1'
})
category_id = category.index(i[3])
threadLocal.s.post(
'***/***',
json={
'category_id': category_id,
'name': i[1],
'author': i[4],
})
for i in reader:
pool.submit(push, i)

