from threading import Thread
def thread_func(name):
for i in range(10):
print(name, i)
# 主线程中创建了3个子线程
if __name__ == '__main__':
thread1 = Thread(target=thread_func, args=('张三',)) # 参数是元组
thread2 = Thread(target=thread_func, args=('李四',)) # 参数是元组
thread3 = Thread(target=thread_func, args=('王五',)) # 参数是元组
thread1.start()
thread2.start()
thread3.start()
print("主线程")
续承Thread类
from threading import Thread
class ThreadFunc(Thread):
def __init__(self, name):
super(ThreadFunc, self).__init__()
self.name = name
# 重写run函数
def run(self):
for i in range(10):
print(self.name, i, sep="---")
# 主线程中创建了3个子线程
if __name__ == '__main__':
thread1 = ThreadFunc('张三')
thread2 = ThreadFunc('李四')
thread3 = ThreadFunc('王五')
thread1.start()
thread2.start()
thread3.start()
print("主线程")
from concurrent.futures import ThreadPoolExecutor
def work_thread(name):
for i in range(8):
print(name)
if __name__ == '__main__':
with ThreadPoolExecutor(8) as thread_pool:
for i in range(5):
thread_pool.submit(work_thread, f"岳王{i}")
import time
from concurrent.futures import ThreadPoolExecutor
def work_thread(name, t):
time.sleep(t)
# print("you can call me ", name)
return name
def func_return(outcome):
print(outcome.result())
if __name__ == '__main__':
with ThreadPoolExecutor(8) as thread_pool:
# 分别睡3秒、1秒、2秒
thread_pool.submit(work_thread, "岳王",3).add_done_callback(func_return)
thread_pool.submit(work_thread, "张飞",1).add_done_callback(func_return)
thread_pool.submit(work_thread, "刘备",2).add_done_callback(func_return)
import time
from concurrent.futures import ThreadPoolExecutor
def work_thread(name, t):
time.sleep(t)
# print("you can call me ", name)
return name
def func_return(outcome):
print(outcome.result())
if __name__ == '__main__':
with ThreadPoolExecutor(8) as thread_pool:
# 分别睡3秒、1秒、2秒
res = thread_pool.map(work_thread, ['岳王', '张飞', '刘备'], [3, 1, 2])
for i in res:
print(i)
from concurrent.futures import ThreadPoolExecutor
import requests
from lxml import etree
def get_data(page):
data = {
'limit': '20',
'current': page,
'pubDateStartTime':'',
'pubDateEndTime':'',
'prodPcatid': '1189',
'prodCatid':'',
'prodName':'',
}
url = '仅做格式http://www.xinfadi.com.cn/getPriceData.html未曾试用'
res = requests.post(url, data=data).json()
for i in res['list']:
# print(i)
one_row = i['prodCat']+','+i['prodPcat']+','+i['prodName']+','+i['lowPrice']+','+i['avgPrice']+','+i['highPrice']+','+i['specInfo']+','+i['place']+','+i['unitInfo']+','+i['pubDate']
f.write(one_row)
f.write('\n')
f = open('price.csv', 'w', encoding='utf-8')
if __name__ == '__main__':
with ThreadPoolExecutor(8) as thread_pool:
for page in range(1, 8):
thread_pool.submit(get_data, page)
# print(res['list'])