this program designs the communication diagram, as shown in the communication model diagram.
the problem is that the online_tcp_ chat module and the manager_new_tcp module cannot communicate with multiple processes through the online_chat_queue module.
both can run normally and independently, but the message that another program put enters into the queue cannot be taken out. Has been blocking
picture description
File-online_tcp_ chat module
import socket
import threading
import logging
import online_chat_queue
logging.basicConfig (level=logging.INFO, format= "% (threadName)-10s% (message) s")
class OnlineChatModuleOne (object):
def __init__(self):
-sharp
self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-sharp
self.tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.tcp_socket.bind(("127.0.0.1", 7890))
self.tcp_socket.listen(128)
self.th_sta = False
def run(self):
while True:
logging.info("")
new_client, new_addr = self.tcp_socket.accept()
-sharp
logging.info("")
th_recv = threading.Thread(name="th_recv+%s" % str(new_addr), target=self.recv_msg, args=(new_client,))
th_send = threading.Thread(name="th_send+%s" % str(new_addr), target=self.send_msg, args=(new_client,))
-sharp
self.th_sta = False
th_recv.start()
th_send.start()
logging.info("")
def recv_msg(self, new_socket):
while True:
logging.info("")
recv_data = new_socket.recv(1024).decode("gbk")
-sharp if not recv_data:
-sharp online_chat_queue.msg_son_2_par.put("0000")
-sharp new_socket.close()
-sharp self.th_sta=True
-sharp break
online_chat_queue.msg_son_2_par.put(recv_data)
print(online_chat_queue.msg_son_2_par.qsize())
logging.info(",%s" % recv_data)
-sharp logging.info("recv_data\t %s" % online_chat_queue.msg_son_2_par.get())
def send_msg(self, new_socket):
while True:
logging.info(">")
-sharp send_data=input("%s:"%threading.currentThread).encode("utf-8")
send_data = online_chat_queue.msg_par_2_son.get()
logging.info("send_data%s" % send_data)
new_socket.send(send_data.encode("utf-8"))
logging.info("")
if self.th_sta:
break
def main ():
-sharp
onlinechat = OnlineChatModuleOne()
-sharp
onlinechat.run()
if name ="_ _ main__":
main()
second module online_chat_queue
"
"
from multiprocessing import Queue
msg_son_2_par = Queue ()
< H1 > the user input message obtained by the main process from the web page is sent to the child process < / H1 >msg_par_2_son = Queue ()
third module manager_new_tcp
< H1 > as an intermediate management zhe between tcp--socket and web pages < / H1 > import threading
import online_chat_queue
import logging
logging.basicConfig (level=logging.INFO,format= ("% (threadName)-10s% (message) s")
import time
import onLine_tcp_ chat
import multiprocessing
class ManagerOnlineQueue (object):
def run(self):
-sharp
th_send=threading.Thread(target=self.send_queue)
th_recv=threading.Thread(target=self.recv_queue)
-sharp
th_recv.start()
th_send.start()
logging.info("")
def send_queue(self):
while True:
logging.info("queue")
send_data=input("")
online_chat_queue.msg_par_2_son.put(send_data)
logging.info("msg_par_2_son %s " % send_data )
def recv_queue(self):
while True:
logging.info("")
logging.info("queue")
recv_data=online_chat_queue.msg_son_2_par.get()
logging.info(".%s" % recv_data)
def main ():
onlinech=onLine_tcp_.OnlineChatModuleOne()
pro_onsocket=multiprocessing.Process(target=onlinech.run)
pro_onsocket.start()
manager=ManagerOnlineQueue()
manager.run()
if name ="_ _ main__": main ()