【问题标题】:How to loop through array 10 at a time using threads?如何使用线程一次循环遍历数组 10?
【发布时间】:2020-12-05 21:22:04
【问题描述】:

我目前正在创建一个 python 脚本,它将遍历从列表中接收的数组(使用 readlines()),然后使用 python 线程一次循环遍历这十个。到目前为止,我已经尝试了几个选项

  1. for循环卡号for num in numbers:,然后使用另一个for循环for i in range(10):创建10个线程。这种方法不起作用,因为它只创建了 10 个线程

  2. 我当前的代码

for card in cards:
    threads = []
    card = card.strip()
    for i in range(10):
        x = threading.Thread(target=testFunct, args=(card,))
        threads.append(x)
        x.start()
        for thread in threads:
            thread.join()

非常感谢任何帮助。我真的不知道该怎么做。我在想我可能会使用像线程池这样的东西,但我不确定它是如何工作的。

【问题讨论】:

  • 提供cards的样本列表?
  • [143244142344324, 43412342144, 41324134124445, 456736275657, 1431432166546] 这是用户输入的,所以长度不完全知道@coderoftheday
  • 您是在问如何将cards list 分成 10 组吗?这与多线程并没有太大关系……
  • 把带有thread.join()的'for'从创建线程的'for'中去掉。
  • @DanielFarina 说的很有效,谢谢!

标签: python multithreading python-multithreading


【解决方案1】:
for card in cards:
    threads = []
    card = card.strip()
    for i in range(10):
        x = threading.Thread(target=testFunct, args=(card,))
        threads.append(x)
        x.start()
    for thread in threads:
        thread.join()

@DanielFarina 的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2013-11-15
    • 2015-11-10
    • 2021-09-08
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多