Python3.6 多线程执行,apply函数替代品

qq_36191093 2018-11-22 12:30:55
根据网上代码写的多线程不起作用,链接:https://www.cnblogs.com/yeayee/p/4952022.html


源码:

import threading
from time import ctime, sleep


class MyThread(threading.Thread):

def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args

def run(self):
# apply(self.func, self.args) # 用不了...
# function(self.func,self.args,self.name)
pass



def super_play(file, time):
for i in range(2):
print('开始运行: %s! %s' % (file, ctime()))
sleep(time)

list = {'爱情买卖.mp3':2, '雷神.mp4':5}

threads = []
files = range(len(list))

for k,v in list.items():
t = MyThread(super_play,(k,v),super_play.__name__)
threads.append(t)

if __name__ == '__main__':
for i in files:
threads[i].start()

for i in files:
threads[i].join()


print('结束 %s' % ctime())


执行结果: super_play函数没有执行,直接print了结束

Python3.6版本无法调用apply函数的话,run中该怎么写...

小白自学弄不懂这个,路过的大神帮忙指点一些~谢谢啦


...全文
258 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36191093 2018-11-22
  • 打赏
  • 举报
回复


路过的段友请注意,滴~滴滴 。驻足留言解题一条龙服务
qq_36191093 2018-11-22
  • 打赏
  • 举报
回复 1
自己占个楼加加人气~~~希望可以认识某位Python大神,大家交流谈心 (技术问题不会可以谈谈感情问题)
qq_36191093 2018-11-22
  • 打赏
  • 举报
回复 1
引用 3 楼 hskill 的回复:
你的run没写调用啊, 当然不执行了

def run(self):
self.func(*self.args)
pass



多谢老铁回复,
  • 打赏
  • 举报
回复
你的run没写调用啊, 当然不执行了

    def run(self):
        self.func(*self.args)
        pass

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧