0

I Have written Python script using socket. I wrote a basic chat, but it has a problem, I dunno how to use the Threading library as well to make the Client side to work without blocking. I tryed with While but it says that:

thread.error: can't start new thread

This is the client code:

import socket
import threading

my_socket = socket.socket()
my_socket = socket.socket()
my_socket.connect(('127.0.0.1', 23))
print('Welcome to the chat room . You can send messages here.')
print('Choose a nickname.')
nickname = raw_input()


def rcv_msg():
    print  (my_socket.recv(1024))


def snd_msg():
    txt = raw_input()
    my_socket.send('[' + nickname + ']: ' + txt)
while True:
    recv_thread = threading.Thread(target=rcv_msg)
    recv_thread.start()
    send_thread = threading.Thread(target=snd_msg)
    send_thread.start()

How can I make it work?

  • 2
    There is a thread limit. You try to generate enormous amounts of threads in your while loop. – Daniel May 31 '17 at 21:59
  • @pstatix how do I make the thread able to keep waiting for new msgs, i mean if its not in while, after 1 time client getting msg, the proccses gets end. –  May 31 '17 at 22:05
  • @pstatix thank you for the quick response, can you please write the code? if its too much its ok. –  May 31 '17 at 22:11

2 Answers2

0

You have no end condition on that while loop, so you are going to create a lot of threads, using up all your system resources.

See previous answer

Jerry Zhao
  • 184
  • 11
0

You shouldn't create and start new threads in infinite loop. As Jerry mentioned it's going to make new threads all the time and use all your system resources.

If you aim to make those threads running continuously you could create loops inside send_msg() and rcv_msg() definition.

Qback
  • 4,310
  • 3
  • 25
  • 38