Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:线程不允许程序的其余部分运行_Python_Multithreading - Fatal编程技术网

Python:线程不允许程序的其余部分运行

Python:线程不允许程序的其余部分运行,python,multithreading,Python,Multithreading,在我的程序中,当一个线程启动时,只有该线程运行,而程序的其余部分等待它。节目如下: import socket from time import sleep import threading import string class IRC: "IRC Module for python" def __init__(self, HOST, PORT, NICK, REALNAME, IDENT): print "New Connection" se

在我的程序中,当一个线程启动时,只有该线程运行,而程序的其余部分等待它。节目如下:

import socket
from time import sleep
import threading
import string

class IRC:
    "IRC Module for python"
    def __init__(self, HOST, PORT, NICK, REALNAME, IDENT):
        print "New Connection"
        self.s=socket.socket( )
        self.s.connect((HOST, PORT))
        self.s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
        self.s.send("NICK %s\r\n" % NICK)
        self.rb="ReadBuffer"
        t = threading.Thread(target=self.ping())
        t.start()
        print "started thread"
    def read(self):
        readbuffer=readbuffer+self.s.recv(1024)
        temp=string.split(readbuffer, "\n")
        readbuffer=temp.pop( )
        return temp
    def ping(self):
        "Handles Pinging"
        readbuffer=""
        print "Started pinging"
        while True:
            readbuffer=readbuffer+self.s.recv(1024)
            temp=string.split(self.rb, "\n")
            readbuffer=temp.pop( )
            for line in temp:
                line=string.rstrip(line)
                line=string.split(line)
                if(line[0]=='PING'):
                    self.s.send("PONG %s\r\n" % line[1])
                    print("Ponged")
                    self.pinged=True
            print "ran"
    def send(self, message, channel):
        self.s.send("PRIVMSG %s :%s\r\n" % (channel, message))

既然评论已经解决了你的问题,就没什么好说的了。下面是我所说的:

类threading.Thread(组=None,目标=None,名称=None,args=(), kwargs={},*,守护进程=None)

应该始终调用此构造函数 使用关键字参数。论点如下:

组应为无;当线程组 类被实现

target是run()方法调用的可调用对象。 默认为“无”,表示不调用任何内容


因此,您应该将
target
设置为可调用的,即
self.ping
方法,而不是调用它。

t=threading.Thread(target=self.ping())
这将在主线程中调用
self.ping
,并尝试将其返回值传递给
Thread
。尝试
Thread(target=self.ping)
它正在工作。非常感谢。