0

The problem I've got right now is one concerning this chat client I've been trying to get working for some days now. It's supposed to be an upgrade of my original chat client, that could only reply to people if it received a message first.

So after asking around and researching people I decided to use select.select to handle my client.

Problem is it has the same problem as always.

*The loop gets stuck on receiving and won't complete until it receives something*

Here's what I wrote so far:

import select
import sys #because why not?
import threading
import queue

print("New Chat Client Using Select Module")

HOST = input("Host: ")
PORT = int(input("Port: "))

s = socket(AF_INET,SOCK_STREAM)

print("Trying to connect....")
s.connect((HOST,PORT))
s.setblocking(0)
# Not including setblocking(0) because select handles that. 
print("You just connected to",HOST,)

# Lets now try to handle the client a different way!

while True:
    #     Attempting to create a few threads
    Reading_Thread = threading.Thread(None,s)
    Reading_Thread.start()
    Writing_Thread = threading.Thread()
    Writing_Thread.start()



    Incoming_data = [s]
    Exportable_data = []

    Exceptions = []
    User_input = input("Your message: ")

    rlist,wlist,xlist = select.select(Incoming_data,Exportable_data,Exceptions)

    if User_input == True:
        Exportable_data += [User_input]

Your probably wondering why I've got threading and queues in there.

That's because people told me I could solve the problem by using threading and queues, but after reading documentation, looking for video tutorials or examples that matched my case. I still don't know at all how I can use them to make my client work.

Could someone please help me out here? I just need to find a way to have the client enter messages as much as they'd like without waiting for a reply. This is just one of the ways I am trying to do it.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Micrified
  • 3,338
  • 4
  • 33
  • 59

1 Answers1

1

Normally you'd create a function in which your While True loop runs and can receive the data, which it can write to some buffer or queue to which your main thread has access.

You'd need to synchronize access to this queue so as to avoid data races.

I'm not too familiar with Python's threading API, however creating a function which runs in a thread can't be that hard. Lemme find an example.

Turns out you could create a class with a function where the class derives from threading.Thread. Then you can create an instance of your class and start the thread that way.

class WorkerThread(threading.Thread):

    def run(self):
        while True:
            print 'Working hard'
            time.sleep(0.5)

def runstuff():
    worker = WorkerThread()
    worker.start() #start thread here, which will call run()

You can also use a simpler API and create a function and call thread.start_new_thread(fun, args) on it, which will run that function in a thread.

def fun():
    While True:
        #do stuff

thread.start_new_thread(fun) #run in thread.
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • If only I knew what class's were, or functions. Perhaps I should just give up. I don't know anything anymore. Documentation is 100% ineffective to me. – Micrified Mar 08 '13 at 17:54
  • @Xeon a class is just a way to define a type of something. It describes what the type has and what operations can be done on the type. A class person, may have a member name, address, etc. and may have an operation called "goHome" or "work". – Tony The Lion Mar 08 '13 at 17:56
  • I still don't understand what is going on. Its so frustrating as its been 2 days of looking for a fix and I'm just despairing now. Even with your help I have no idea what I'm doing. Or even how to use this. I need a book. Or a proper tutorial link. I've looked everywhere and can find nothing. – Micrified Mar 08 '13 at 18:05
  • What's exactly the problem you're running into? – Tony The Lion Mar 08 '13 at 18:37
  • The problem I am running into is that I do not understand how I am supposed to integrate threads and queues with my current problem. People told me it's the solution. But other than that I am completely clueless as to how to make it work. I tried making a few threads, but then I was lost as to what I was supposed to do with the thread, how to integrate it with my input. How to make select.select send off my replies when they were ready to be sent off. How would it even solve my current problem, that being that I was stuck waiting for to recv data. And it keeps getting more and more obscure – Micrified Mar 08 '13 at 18:58
  • In fact, I tried running this just to see if it actually got past my select.select part, and it doesn't. It gets struck immediately after entering the message. I'm screwed – Micrified Mar 08 '13 at 19:09
  • @Xeon I think you really need to buy yourself a good programming book and learn from the beginning. Threading is hard, and if you don't really get it, it's only making things harder. – Tony The Lion Mar 08 '13 at 19:33
  • I think this may be [helpful.](http://www.amazon.co.uk/Beginning-Python-Professional-Edition-Professionals/dp/1590599829/ref=sr_1_11?ie=UTF8&qid=1362772224&sr=8-11) – Tony The Lion Mar 08 '13 at 19:51