AB49K
AB49K

Reputation: 3415

Python threading only launching one extra thread

import socket
import thread
s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)
s.connect(("server", 6661))
def recv():
    while 1:
        print(s.recv(1024))
def send():
    while 1:
        msg = raw_input("> ")
        s.send(msg)
thread.start_new_thread(recv())
thread.start_new_thread(send())

Why does the code not run after thread recv() - I can't see where it should hang

Upvotes: 4

Views: 4007

Answers (1)

falsetru
falsetru

Reputation: 369394

Adjust as follow:

thread.start_new_thread(recv, ())
thread.start_new_thread(send, ())

By appending () right after the function name, you call recv and send in main thread, not in new thread.

Upvotes: 8

Related Questions