스레딩 모듈을 사용하는 파이썬에서, 내 프로그램은 내가 동시에 만든 2 개의 스레드를 실행하지 않습니다. 나는 P2P를 사용하여 인스턴트 메시지 프로그램을 만들려고하는데 문제가 소켓 모듈과 관련이 있는지 또는 잘못된 것을하고 있는지 알지 못합니다. 코드 :스레드가 동시에 실행되지 않습니다. (Python)
import socket
import threading
class Receiver:
def __init__(self):
# Create socket that supports IPv4, TCP Protocol
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket created."
# Requests for IP of host (DNS)
dns = "localhost"
HOST = ''
PORT = 57492
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT)) # Listens on all interfaces...
print 'Listening on port 25565'
s.listen(True) # Listen on the newly created socket...
conn, addr = s.accept()
print 'Connected in port 25565'
global data
while 1:
data = conn.recv(1024)
print data
s.close()
def Sender(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dns = "localhost"
HOST = socket.gethostbyname(dns)
PORT = 57492
# Connect to server
s.connect((HOST, PORT))
print "Socket connected to " + dns + " on IP " + HOST
while 1:
# Assign message to be sent to server to a variable
message = raw_input("Message to be sent to server: ")
#Send the actual message to server
s.sendall(message)
print "Message sent successfully"
s.close()
def go(self):
th1 = threading.Thread()
th2 = threading.Thread(target=self.Sender)
th1.start()
th2.start()
t = Receiver()
t.go()
당신이 무엇을 기대를'th1'는 할까? 당신은'target'을주지 않았기 때문에 당신이 그것을 만든 직후에 죽습니다. – roippi