V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsds
V2EX  ›  Python

Python 类中的方法如何多线程调用?

  •  
  •   wsds · 2018-04-24 20:58:07 +08:00 · 3009 次点击
    这是一个创建于 2164 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这么写直接弹框报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 条附言  ·  2018-04-25 16:09:20 +08:00
    10 条回复    2018-04-25 19:28:55 +08:00
    nitro123
        1
    nitro123  
       2018-04-25 10:48:50 +08:00 via iPhone
    pool.map ?
    wsds
        2
    wsds  
    OP
       2018-04-25 10:51:08 +08:00
    @nitro123 哪尼?
    chenstack
        3
    chenstack  
       2018-04-25 11:56:44 +08:00
    wsds
        4
    wsds  
    OP
       2018-04-25 15:56:36 +08:00
    @chenstack 我测试了一下,这样是挨个执行啊,没有并行执行啊,这是单线程吧
    wsds
        5
    wsds  
    OP
       2018-04-25 16:04:30 +08:00
    wsds
        6
    wsds  
    OP
       2018-04-25 16:10:33 +08:00
    @chenstack 你的图是怎么回复的?
    chenstack
        7
    chenstack  
       2018-04-25 16:57:36 +08:00
    把 t.join()注释后也许是你想要的效果。join 的作用是保证当前线程执行完成后,再执行其它线程。
    回复图片直接贴网址就行
    wsds
        8
    wsds  
    OP
       2018-04-25 17:48:43 +08:00
    @chenstack 要把 t.setDaemon(True)也注释掉或者传为 False 才行,但感觉这不是一个完整的多线程了
    chenstack
        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()放在循环外面会出错大概是因为,有些线程设置为守护线程,但主线程退出后那些子线程还未结束。
    wsds
        10
    wsds  
    OP
       2018-04-25 19:28:55 +08:00
    @chenstack 分开确实可以。。。另外,我帖的只是些伪代码,实际当中还操作了数据库,主要报错原因是多线程查询 数据库报错,还没搞定,心塞_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4929 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:50 · PVG 17:50 · LAX 02:50 · JFK 05:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.