How to handle chat client using threading and queues?

老子叫甜甜 提交于 2019-12-08 10:44:17

问题


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.


回答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.


来源:https://stackoverflow.com/questions/15297296/how-to-handle-chat-client-using-threading-and-queues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!