1

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

4

3 回答 3

1

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

于 2013-04-02T08:51:19.000 回答
1

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

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)
于 2013-04-02T08:55:02.657 回答
0

您需要提到的 ATOzTOA 之类的锁。您通过以下方式创建它们

lock = threading.Lock()

并且线程在进入临界区时获取它们。完成部分后,他们释放锁。写这个的pythonic方法是

with lock:
   do_something(buffer)
于 2013-04-02T08:56:36.237 回答