这么写直接弹框报python
运行出错,/尴尬,那这个怎么多线程调用getA/getB/getC/getD/getE
呢?每次传参还要循环个arg2list
列表
class ThreadTest():
def __init__(self):
pass
def getA(self, args1, args2):
pass
def getB(self, args1, args2):
pass
def getC(self, args1, args2):
pass
def getD(self, args1, args2):
pass
def getE(self, args2):
pass
if __name__ == "__main__":
Test = ThreadTest()
args2list = ['table1','table2']
args1 = 2
thread_ = []
for args2 in args2list:
t1 = threading.Thread(target=Test.getA, args = (args1, args2))
t2 = threading.Thread(target=Test.getB, args = (args1, args2))
t3 = threading.Thread(target=Test.getE, args = (args2))
t4 = threading.Thread(target=Test.getC, args = (args1, args2))
t5 = threading.Thread(target=Test.getrace, args = (args1, args2))
thread_.append(t1)
thread_.append(t2)
thread_.append(t3)
thread_.append(t4)
thread_.append(t5)
print(thread_)
for t in thread_:
t.setDaemon(True)
t.start()
t.join()
1
nitro123 2018-04-25 10:48:50 +08:00 via iPhone
pool.map ?
|
3
chenstack 2018-04-25 11:56:44 +08:00
|
5
wsds OP @chenstack
![2M537N.png]( https://t1.picb.cc/uploads/2018/04/25/2M537N.png) ![2M5aZs.png]( https://t1.picb.cc/uploads/2018/04/25/2M5aZs.png) |
7
chenstack 2018-04-25 16:57:36 +08:00
把 t.join()注释后也许是你想要的效果。join 的作用是保证当前线程执行完成后,再执行其它线程。
回复图片直接贴网址就行 |
8
wsds OP @chenstack 要把 t.setDaemon(True)也注释掉或者传为 False 才行,但感觉这不是一个完整的多线程了
|
9
chenstack 2018-04-25 19:09:14 +08:00
可以把 start 和 join 分开
for t in thread_: t.setDaemon(True) t.start() for t in thread_: t.join() 另外你一开始那样把 t.join()放在循环外面会出错大概是因为,有些线程设置为守护线程,但主线程退出后那些子线程还未结束。 |
10
wsds OP @chenstack 分开确实可以。。。另外,我帖的只是些伪代码,实际当中还操作了数据库,主要报错原因是多线程查询 数据库报错,还没搞定,心塞_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
|