代码之家  ›  专栏  ›  技术社区  ›  Kevin Mayo Pavel Krčmář

有没有可能变得更像蟒蛇?

  •  -1
  • Kevin Mayo Pavel Krčmář  · 技术社区  · 6 年前

    我创建了五个线程来做一些事情。

    threadList = [threading.Thread(target=want) for i in range(5)] # create five threads and save it in a list.
    for i in threadList: # use for loop to start them.
        i.start()
    

    是否可以启动这些线程并将它们保存在 list 同时?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Simon W    6 年前

    首先,我要说的是,简单地使用for循环而不是列表生成器将被认为更具python特性,因为它增加了可读性。

    from multiprocessing.dummy import Pool as ThreadPool
    
    threads = 5
    pool = ThreadPool(threads)
    results = pool.map(want)
    pool.close()
    pool.join()
    
        2
  •  1
  •   Xantium    6 年前

    浓缩这并不是更像蟒蛇。阅读会变得更加混乱,如果你像这样写上几百行,我可能会成为一个巨大的麻烦,试图弄清楚到底发生了什么。

    你可能想退房 pep-8