Issue with python multi threading and socket connections

FlowR

I recently picked up python and I am trying to learn more about networking. I came across a problem, where I am trying to both listen and send data at the same time using multi threading. Here is the code:

import socket
from threading import Thread


name = ""


s = socket.socket()

def Main():
    print("What is your name?")
    name = input("Name: ")
    Connect()

def Connect():
    host = '127.0.0.1'
    port = 5000


    s.connect((host, port))
    Thread(target=Send()).start()
    print("oh")
    Listen()


def Listen():
    while True:
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        if not data:
            break
        print(data)

def Send():
    message = input(name + ": ")
    while message != 'q':
        s.send(message.encode("utf-8"))
        message = input(name + ": ")
    s.close()
Main()

The problem is that when the Thread is created, the program is stuck in the while loop of the Send() function, and it never calls the Listen() function. I added a print() function to debug whether anything happens after the thread is created.

This is the output that I got:

What is your name?

Name: FlowR

: test

:

Yuankun

You are calling the Send() function instead of passing it as parameters, that's why it blocks.

Drop the () after target=Send():

Thread(target=Send).start()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multi threading in python : issue with parallel processing

From Dev

Python multi-threading performance issue related to start()

From Dev

Multi-Threading Issue in C

From Dev

Multi-Threading Issue in C

From Dev

Python threading timer issue

From Dev

Python Threading Issue on Windows

From Dev

Python Threading Parameters Issue

From Dev

Multi-threading client/server, Socket Exception

From Dev

java GUI multi-threading with socket

From Dev

WPF multi-level binding threading issue

From Dev

java multi threading issue ( same run time )

From Dev

Integer Casting Issue with Multi-threading in C

From Dev

python - multi-threading in a for loop

From Dev

Multi threading using Python and pymongo

From Dev

Python Feedparser and Multi-threading

From Dev

Multi-threading in Python and shell

From Dev

python - multi-threading in a for loop

From Dev

Python threading global variable issue

From Dev

How to read request from socket using multi-threading

From Dev

Managing database connections in multi-threaded socket server

From Dev

multi thread issue in Python

From Dev

Multi threading in python using parallel threads

From Dev

Multi threading read and write file using python

From Dev

Python how to use Threading in multi download

From Dev

Multi-threading for downloading NCBI files in Python

From Dev

Using a for loop along with multi threading python

From Dev

(probably) Quite simple Java multi-threading issue

From Dev

Multi-threading Issue - Passing objects between threads

From Dev

How to refresh textview based on user input? Multi-threading Issue