python 怎样用threading多线程处理同一数据

python 2.7

我是新手遇到多线程问题

怎样用threading多线程处理同一数据打印?

函数a的内容是输出1-100
想用threading方法,开5条线程同时完成1-100的输出

但我做代码效果是5条线程各自独立完成1-100的输出,请高手指导一下。

import threading
def a():
    for i in range(1,100):
        print i
for i in range(5):
    th=threading.Thread(target=a,args=())
    th.start()
阅读 8.1k
4 个回答

将a设置为全局变量,让五个线程都可以访问,然后开启五个线程,对进行a=a+1操作,并且输出,当a>100后,就停止。多线程记得给a加锁,不然可能不准确。

import logging, threading
from queue import Queue

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(message)s')
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)

shared_queue = Queue()
queue_condition = threading.Condition()
queue_lock = threading.Lock()


def print_task(condition):
    with condition:
        while True:
            if shared_queue.empty():
                logger.info("[%s] - waiting for elements in queue.." % threading.current_thread().name)
                condition.wait()
            else:
                value = shared_queue.get()
                if value > 100:
                    break
                logger.debug("[%s] print [%d]" % (threading.current_thread().name, value))

def print_task_by_lock():
    while True:
        with queue_lock:
            if shared_queue.empty():
                logger.info("[%s] - waiting for elements in queue.." % threading.current_thread().name)
            else:
                value = shared_queue.get()
                if value > 100:
                    break
                logger.debug("[%s] print [%d]" % (threading.current_thread().name, value))

def put(condition):
    with condition:
        for i in range(1, 200):
            shared_queue.put(i)
            logger.debug("put element [%d] into queue.." % i)
            condition.notifyAll()

def put_by_lock():
    with queue_lock:
        for i in range(1, 200):
            shared_queue.put(i)
            logger.debug("put element [%d] into queue.." % i)

# producer = threading.Thread(daemon=True, target=put, args=(queue_condition,))
producer = threading.Thread(daemon=True, target=put_by_lock, args=())
producer.start()

# consumers = [threading.Thread(daemon=True, target=print_task, args=(queue_condition,)) for i in range(4)]
consumers = [threading.Thread(daemon=True, target=print_task_by_lock, args=()) for i in range(4)]
[consumer.start() for consumer in consumers]

[consumer.join() for consumer in consumers]

利用全局set变量来协调各个线程,这样简单点,易理解。

import threading

s=set() # 全局变量

def a():
    for i in range(1,100):
        if i not in s: # 输出过的数字就不打印了
            s.add(i) # 向集合里添加元素,不会影响数据的准确性
            print (i)

            
for i in range(5):
    th=threading.Thread(target=a,args=())
    th.start()

估计你是想要这个

import multiprocessing

def a(i):
    process_name = multiprocessing.current_process().name
    print i, process_name

pool = multiprocessing.Pool(5)

pool.map(a, [i for i in range(1,100)])

pool.close()
pool.join()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进