这个程序设计通信图,如通信模型图展示的。
问题是:online_tcp_聊天模块和manager_new_tcp模块无法通过online_chat_queue的模块,实现多进程通信。
两者都可以自己正常独立进行运行,但是,另一个程序put进去queue中的消息,另外一个程序,无法取出。一直在堵塞
文件一 online_tcp_聊天模块
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):
# 主线程创建套接字,监听连接到来。
self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 设置地址复用
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()
# 创建两个线程处理新的连接
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,))
# 启动两个线程
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")
# if not recv_data:
# online_chat_queue.msg_son_2_par.put("0000")
# new_socket.close()
# self.th_sta=True
# 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)
# logging.info("recv_data\t %s" % online_chat_queue.msg_son_2_par.get())
def send_msg(self, new_socket):
while True:
logging.info("已经来到了处理发送消息的地方>")
# 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():
# 创建对象,
onlinechat = OnlineChatModuleOne()
# 运行方法
onlinechat.run()
if __name__ == '__main__':
main()
第二个模块 online_chat_queue
'''
这是每个进程进行通信采用的序列
'''
from multiprocessing import Queue
# 子进程发送收到的消息给父进程
msg_son_2_par = Queue()
# 主进程从网页获取的用户输入消息,发送给子进程
msg_par_2_son = Queue()
第三个模块 manager_new_tcp
问题是:online_tcp_聊天模块和manager_new_tcp模块无法通过online_chat_queue的模块,实现多进程通信。
两者都可以自己正常独立进行运行,但是,另一个程序put进去queue中的消息,另外一个程序,无法取出。一直在堵塞
![图片说明](https://img-ask.csdn.net/upload/201806/06/1528291610_231138.png)
文件一 online_tcp_聊天模块
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):
# 主线程创建套接字,监听连接到来。
self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 设置地址复用
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()
# 创建两个线程处理新的连接
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,))
# 启动两个线程
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")
# if not recv_data:
# online_chat_queue.msg_son_2_par.put("0000")
# new_socket.close()
# self.th_sta=True
# 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)
# logging.info("recv_data\t %s" % online_chat_queue.msg_son_2_par.get())
def send_msg(self, new_socket):
while True:
logging.info("已经来到了处理发送消息的地方>")
# 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():
# 创建对象,
onlinechat = OnlineChatModuleOne()
# 运行方法
onlinechat.run()
if __name__ == '__main__':
main()
第二个模块 online_chat_queue
'''
这是每个进程进行通信采用的序列
'''
from multiprocessing import Queue
# 子进程发送收到的消息给父进程
msg_son_2_par = Queue()
# 主进程从网页获取的用户输入消息,发送给子进程
msg_par_2_son = Queue()
第三个模块 manager_new_tcp
#作为tcp——socket 和 网页的中间管理zhe import threading import online_chat_queue import logging logging.basicConfig(level=logging.INFO,format=("%(threadName)-10s %(message)s")) import time import onLine_tcp_聊天 import multiprocessing class ManagerOnlineQueue(object): def run(self): #创建两个线程 th_send=threading.Thread(target=self.send_queue) th_recv=threading.Thread(target=self.recv_queue) #启动两个线程 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()