需要访问单个资源的多个线程

时间:2013-04-02 08:49:22

标签: python python-2.7

我目前正在开发一个程序,其中多个线程需要访问单个数组列表。该数组用作“缓冲区”。一个或多个线程写入此列表,一个或多个其他线程从此列表中读取和删除。我的第一个问题是,Python线程中的数组是否安全?如果没有,处理情况的标准方法是什么?

3 个答案:

答案 0 :(得分:1)

如果只有一个资源,请尝试使用Threading.lock

答案 1 :(得分:1)

您应该使用queue lib。 here是一篇很好的文章,解释了线程和队列。

import Queue
import threading
import urllib2
import time
from BeautifulSoup import BeautifulSoup

hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
        "http://ibm.com", "http://apple.com"]

queue = Queue.Queue()
out_queue = Queue.Queue()

class ThreadUrl(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, queue, out_queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            host = self.queue.get()

            #grabs urls of hosts and then grabs chunk of webpage
            url = urllib2.urlopen(host)
            chunk = url.read()

            #place chunk into out queue
            self.out_queue.put(chunk)

            #signals to queue job is done
            self.queue.task_done()

class DatamineThread(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, out_queue):
        threading.Thread.__init__(self)
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            chunk = self.out_queue.get()

            #parse the chunk
            soup = BeautifulSoup(chunk)
            print soup.findAll(['title'])

            #signals to queue job is done
            self.out_queue.task_done()

start = time.time()
def main():

    #spawn a pool of threads, and pass them queue instance
    for i in range(5):
        t = ThreadUrl(queue, out_queue)
        t.setDaemon(True)
        t.start()

    #populate queue with data
    for host in hosts:
        queue.put(host)

    for i in range(5):
        dt = DatamineThread(out_queue)
        dt.setDaemon(True)
        dt.start()


    #wait on the queue until everything has been processed
    queue.join()
    out_queue.join()

main()
print "Elapsed Time: %s" % (time.time() - start)

答案 2 :(得分:0)

你需要像ATOzTOA提到的锁。你可以通过

创建它们
lock = threading.Lock()

如果线程进入临界区,则线程会获取它们。完成该部分后,他们释放锁定。写这个的pythonic方法是

with lock:
   do_something(buffer)