Skip to main content
 首页 » 编程设计

python之将 Python 线程附加到列表

2025年05月04日14jirigala

我正在尝试将线程附加到 Python 中的列表,以便稍后使用 join 方法迭代它们。但是,当我将 Thread 对象附加到列表时,它会在列表中创建两个条目。一个用于线程,一个用于函数对象。 相关代码如下:

#!/usr/bin/python 
 
import socket 
import errno 
import sys 
import operator 
import threading 
import time 
 
def removeDuplicates(oldList): 
    newList = list(); 
 
    for i in oldList: 
        if i not in newList: 
            newList.append(i) 
 
    return newList; 
 
def expandRange(portRange): 
   if "-" not in portRange: 
       l = list() 
       l.append(int(portRange)) 
       return l 
 
   portLimits=portRange.split("-") 
   return range(int(portLimits[0]),int(portLimits[1])+1) 
 
def parsePorts(portList): 
    if "," in portList: 
        ports = portList.split(",") 
        portRanges = [expandRange(i) for i in ports] 
        portRanges = reduce(operator.add, portRanges) 
        ports = removeDuplicates(portRanges) 
        return ports 
        else: 
            l = list() 
            l.append(int(portList)) 
#            print "I AM HERE" ,l 
            return l 
 
 
def check_port(host, port): 
    try: 
        activeThreads.append(id); 
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
        s.connect((host,port)) 
 
        mutex.acquire() 
        print "%s:%s ---> Port open!" %  (host,port) 
        mutex.release() 
 
    except socket.error as e: 
        if e.errno == errno.ECONNREFUSED: 
            mutex.acquire() 
            print "%s:%s ---> Port closed" %  (host,port) 
            mutex.release() 
            s.close() 
        elif e.errno == errno.ETIMEDOUT: 
            mutex.acquire() 
            print "%s:%s ---> Connection timed out" %  (host,port) 
            mutex.release() 
            s.close() 
        elif e.errno == errno.ENOEXEC: 
            mutex.acquire() 
            print "%s:%s ---> Host down" %  (host,port) 
            mutex.release() 
            s.close() 
        else: 
            raise 
 
 
 
if len(sys.argv) < 3: 
    print "Usage is:\n %s <host> <port>" % sys.argv[0] 
    exit(1) 
 
 
host = sys.argv[1] 
portList = sys.argv[2] 
ports = parsePorts(portList); 
activeThreads = list() 
#print activeThreads 
mutex = threading.Lock() 
print "Thread objects: " 
 
 
for port in ports: 
   try: 
       newThread = threading.Thread(target=check_port,args=(host,port)) 
       print type(newThread) 
       activeThreads.append(newThread) 
       newThread.start() 
#       print " THREADS: %s" % activeThreads 
   except: 
      raise 
      #print "Error: unable to start thread" 
 
 
 
print "\nActiveThreads list:" 
#print activeThreads 
counter = 0; 
for t in activeThreads:  
#    print "[%d] => %s" % (counter,type(t)) 
    print counter, t, type(t) 
    counter+=1; 
 
time.sleep(5) 

结果输出如下:

Thread objects: 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
<class 'threading.Thread'> 
 
ActiveThreads list: 
0 <Thread(Thread-1, started -2146345816)> <class 'threading.Thread'> 
1 <built-in function id> <type 'builtin_function_or_method'> 
2 <Thread(Thread-2, started -2146342704)> <class 'threading.Thread'> 
3 <built-in function id> <type 'builtin_function_or_method'> 
4 <Thread(Thread-3, started -2146339968)> <class 'threading.Thread'> 
5 <built-in function id> <type 'builtin_function_or_method'> 
6 <Thread(Thread-4, started -2146337288)> <class 'threading.Thread'> 
7 <built-in function id> <type 'builtin_function_or_method'> 
8 <Thread(Thread-5, started -2146334528)> <class 'threading.Thread'> 
9 <built-in function id> <type 'builtin_function_or_method'> 
10 <Thread(Thread-6, started -2146330960)> <class 'threading.Thread'> 
11 <built-in function id> <type 'builtin_function_or_method'> 
12 <Thread(Thread-7, started -2146328288)> <class 'threading.Thread'> 
13 <built-in function id> <type 'builtin_function_or_method'> 
14 <Thread(Thread-8, started -2146325480)> <class 'threading.Thread'> 
15 <built-in function id> <type 'builtin_function_or_method'> 
16 <Thread(Thread-9, started -2146322832)> <class 'threading.Thread'> 
17 <built-in function id> <type 'builtin_function_or_method'> 
18 <Thread(Thread-10, started -2146320160)> <class 'threading.Thread'> 
19 <built-in function id> <type 'builtin_function_or_method'> 
20 <Thread(Thread-11, started -2146317512)> <class 'threading.Thread'> 
21 <built-in function id> <type 'builtin_function_or_method'> 
22 <Thread(Thread-12, started -2146314760)> <class 'threading.Thread'> 
23 <built-in function id> <type 'builtin_function_or_method'> 
localhost:25 ---> Port closed 
localhost:113 ---> Port closed 
localhost:32 ---> Port closed 
localhost:110 ---> Port closed 
localhost:81 ---> Port closed 
localhost:112 ---> Port closed 
localhost:33 ---> Port closed 
localhost:82 ---> Port closed 
localhost:80 ---> Port closed 
localhost:34 ---> Port closed 
localhost:111 ---> Port closed 
localhost:90 ---> Port closed 

与线程对象一起附加到列表中的这些生成的 builtin_function_or_methods 是什么?我如何才能获得要迭代的线程列表并调用 .join()?

谢谢!

请您参考如下方法:

代码看起来不错。感谢您添加缺少的方法。

你应该在你的 check_port 方法中移除 activeThreads.append(id);,因为 id 是一个 builtin-function python 和缺少的括号会导致 builtin_function_or_method 类型错误。

之后所有线程的连接就很容易了:

for t in activeThreads:  
    t.join()