简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM