如何使用线程和队列处理聊天客户端? [英] How to handle chat client using threading and queues?

查看:89
本文介绍了如何使用线程和队列处理聊天客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在遇到的问题是与这个聊天客户端有关的问题,我一直在努力工作几天。本来应该是我原来的聊天客户端的升级版,只有先收到消息才可以回复别人。

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.

因此,在四处询问和研究人员之后,我决定使用select.select处理我的客户。

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.

推荐答案

通常,您会创建一个函数,其中为True 循环运行并可以接收数据,该数据可以写入到您的主线程可以访问的某些缓冲区或队列中。

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.

我对Python的线程API不太熟悉,但是创建在线程中运行的函数并不难。莱姆找到一个例子。

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.

结果是,您可以创建一个带有函数的类,该类从 threading.Thread 。然后,您可以创建类的实例并以这种方式启动线程。

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()

您还可以使用更简单的API并创建一个函数,然后在其上调用 thread.start_new_thread(fun,args)

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.

这篇关于如何使用线程和队列处理聊天客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆