前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >指定线程数的方式实现python多线程及多线程获取返回值

指定线程数的方式实现python多线程及多线程获取返回值

原创
作者头像
sydjcwx
修改2021-02-19 09:57:46
1.3K0
修改2021-02-19 09:57:46
举报
文章被收录于专栏:测开之路
代码语言:txt
复制
# coding=utf-8
import threading, time


# 1、自己写代码实现,利用全局变量保存线程执行结果



def get_detail_video(vid):
    print('-->', vid)
    time.sleep(2)

datas = []
ths = []

for i in range(10):
    th = threading.Thread(target=get_detail_video, args=(i, datas,))
    th.start()
    ths.append(th)
    if len(ths) >= 3:
        for t in ths:
            t.join()
        ths.clear()
for t in ths:
    t.join()


# 2、重写模块,直接获取每个线程执行结果

# coding=utf-8
from threading import Thread
import time


class MyThread(Thread):
    def __init__(self, func, args):
        super(MyThread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result
        except Exception:
            return None


def a(x, y):
    time.sleep(2)
    return x+y


def b(x, y):
    time.sleep(2)
    return x+y


at = []
t1 = MyThread(a, args=(1,2,))
at.append(t1)
t2 = MyThread(b, args=(10,20,))
at.append(t2)
print(len(at))

ss_tt = time.time()

for t in range(len(at)):
    aa = at[t]
    aa.start()

for t in at:
    t.join()

r1 = t1.get_result()
print(' MyThread__%s 耗时: %s, result = %s' % (1, time.time() - ss_tt, r1))
r2 = t2.get_result()
print(' MyThread__%s 耗时: %s, result = %s' % (2, time.time() - ss_tt, r2))


ss_tt = time.time()
r1 = a(1,2)
print(' MyThread__%s 耗时: %s, result = %s' % (1, time.time() - ss_tt, r1))

r2 = b(10,20)
print(' MyThread__%s 耗时: %s, result = %s' % (2, time.time() - ss_tt, r2))







# 3、 利用线程池 模块

import threadpool


excel_datas = [1, 2, 3]


def run_xhs(i):
    return i


# 回调函数,接受的参数(请求本身,和请求工作函数执行结果)可以省略  
spider_results = []

def get_result(tt_requests, result):
    global spider_results
    spider_results.append(result)

tt_pool = threadpool.ThreadPool(2)    # 指定线程数为2
tt_requests = threadpool.makeRequests(run_xhs, excel_datas, get_result)  # 函数名, 列表
for tt_req in tt_requests:
    tt_pool.putRequest(tt_req)
tt_pool.wait()

if spider_results:
    print('spider_results = ', len(spider_results))
    write_excel_data(spider_results, excel_name)


# spider_results =  3 [1, 2, 3]



 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档