2014-03-05 36 views
0

我需要修改list,通过在线程末尾添加一些元素。如何修改线程内的列表?

这是我的代码:

def go(): 
    while queueCommand.qsize() > 0: 
     command = queueCommand.get(False) 
     res = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True) 
     output = res.communicate() 
     output = str(output) 
     star = output.find("address") + 8 
     en = output.find(",") 
     ip = output[star:en-3] 
     alist.append(ip) #<================== her i use the liste 

if __name__ == '__main__':  
    with open ("icq.txt","r") as myfile: 
    text = myfile.read() 
    end = 0 
    alist = [] 
    queueCommand = Queue.Queue() 
    while True: 
    start = text.find("href=") + 13 
    if start == 12: 
     break 
    end = text.find("/",start) 
    if text[start:end].find("icq.com") != -1: 
     hostname="host "+ text[start:end] 
     queueCommand.put(hostname) 
    text = text[end:len(text)] 

    for i in range(10): 
    threading.Thread(target=go,args=(alist)).start() #<====== i give the list as argument 

    print alist 

最后一个print语句显示一个空的列表,[]。有任何想法吗?

+1

你需要有一些同步,可能与互斥体。 –

+0

我可以有一个使用哑巴的例子,我很抱歉,但我是新的python – user3383192

+0

地方alist = []之外的main,在你的go()函数之上,然后在main中使用它。我建议你在实例化队列时做同样的事情; queueCommand = Queue.Queue()。如果在线程类中使用它并定义一个单独的函数来读取文件,它可能会更容易。这是一个链接到一个可能是有用的SO答案http://stackoverflow.com/questions/6286235/multiple-threads-in-python – user1749431

回答

1

你有几个问题。

  1. 您指定alist为ARGS,但你需要把它作为它看起来像你试图做一个元组,而是一个一个项目的元组是这样的(alist,)。现在你只是使用alist全局,这可能不是你想要的。

  2. 你的方法并不期望一个参数(即alist)。

  3. 为了线程安全,我相信你需要使用某种形式的信号/互斥/锁原语。线程模块附带一个锁定实现,您可以使用该实现在追加操作期间限制对alist的访问。

  4. 最重要的是,在打印结果之前,您并未等待您的线程完成。要等待线程完成,您需要在线程上调用.join()

我可能会选择使用另一个Queue实例将结果放入,然后在线程完成后,您可以从队列中读取全部内容以构建您的列表。

这是您的代码的更新版本(工作)。就像我说的那样,我可能会选择使用Queue,而且自从我切换到eventlet/gevent之后,我还没有使用过线程模块......所以可能有方法来改进我提供的内容。

import threading 
import Queue 
import subprocess 

lock = threading.Lock() 

def go(alist): 
    while queueCommand.qsize() > 0: 
     command = queueCommand.get(False) 
     res = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True) 
     output = res.communicate() 
     output = str(output) 
     star = output.find("address") + 8 
     en = output.find(",") 
     ip = output[star:en-3] 
     lock.acquire() 
     alist.append(ip) #<================== her i use the liste 
     lock.release() 

def foo(alist): 
    alist.append("bar") 

if __name__ == '__main__': 
    with open ("icq.txt","r") as myfile: 
    text = myfile.read() 
    end = 0 
    alist = [] 
    queueCommand = Queue.Queue() 
    while True: 
    start = text.find("href=") + 13 
    if start == 12: 
     break 
    end = text.find("/",start) 
    if text[start:end].find("icq.com") != -1: 
     hostname="host "+ text[start:end] 
     queueCommand.put(hostname) 
    text = text[end:len(text)] 

    threads = [] 
    for i in range(10): 
    thread = threading.Thread(target=go,args=(alist,)) #<====== i give the list as argument) 
    thread.start() 
    threads.append(thread) 
    for thread in threads: 
    thread.join() 

    print alist 
+0

是的,它的工作,谢谢 – user3383192