while循环中的多个线程

时间:2016-12-22 01:12:08

标签: python multithreading

我对以下代码有一个简单的问题/疑问。

findInterval(aMinute, c(0, 15, 30, 45, 60))

我想每次执行10个线程并等待它完成,然后继续接下来的10个线程,直到count< = 255

我理解我的问题以及为什么它为每次计数增加执行10个线程,但不知道如何解决它,任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:0)

有两种可行的选项:multiprocessingThreadPool建议使用{malineau建议并使用queue。这是一个queue的示例,它在10个不同的线程中同时执行请求。请注意,它不执行任何类型的批处理,一旦线程完成它就会选择下一个任务而不关心其他工作人员的状态:

import queue
import threading

def conn():
    try:
        while True:
            ip, port = que.get_nowait()
            print('Connecting to {}:{}'.format(ip, port))
            que.task_done()
    except queue.Empty:
        pass

que = queue.Queue()
for i in range(256):
    que.put(('192.168.0.' + str(i), 80))

# Start workers
threads = [threading.Thread(target=conn) for _ in range(10)]
for t in threads:
    t.start()

# Wait que to empty
que.join()

# Wait workers to die
for t in threads:
    t.join()

输出:

Connecting to 192.168.0.0:80
Connecting to 192.168.0.1:80
Connecting to 192.168.0.2:80
Connecting to 192.168.0.3:80
Connecting to 192.168.0.4:80
Connecting to 192.168.0.5:80
Connecting to 192.168.0.6:80
Connecting to 192.168.0.7:80
Connecting to 192.168.0.8:80
Connecting to 192.168.0.9:80
Connecting to 192.168.0.10:80
Connecting to 192.168.0.11:80
...

答案 1 :(得分:0)

我修改了您的代码,使其具有正确的逻辑来执行您想要的操作。请注意,我没有运行它,但希望您能得到一般的想法:

inspect

答案 2 :(得分:0)

使用concurrents.futures

可以轻松实现

这里是示例代码:

from concurrent.futures import ThreadPoolExecutor 

ip = '192.168.0.'
count = 0

THREAD_COUNT = 10

def work_done(future):
    result = future.result()
    # work with your result here


def main():
    with ThreadPoolExecutor(THREAD_COUNT) as executor:
        while count <= 255:
            count += 1
            ipg=ip+str(count)
            executor.submit(conn, ipg, 80).add_done_callback(work_done)

if __name__ == '__main__':
    main()

这里执行程序返回它提交的每个任务的未来。 请记住,如果您使用add_done_callback()完成的任务从线程返回主线程(这将阻止您的主线程),如果您真的想要真正的并行性,那么您应该等待未来的对象单独。这是代码片段。

from concurrent.futures import ThreadPoolExecutor
from concurrent.futures._base import wait

futures = []
with ThreadPoolExecutor(THREAD_COUNT) as executor:
    while count <= 255:
        count += 1
        ipg=ip+str(count)
        futures.append(executor.submit(conn, ipg, 80))
wait(futures)

for succeded, failed in futures:
    # work with your result here

希望这有帮助!