会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题

from multiprocessing import Process, Queue

from time import sleep



class MyProcess(Process):

    def __init__(self,name,mq):

        Process.__init__(self)

        self.name = name

        self.mq = mq


    def run(self):

        print(f"Process:{self.name},start")

        print(f"get Data:{self.mq.get()}")

        sleep(3)

        print(f"Process:{self.name},end")


if __name__ == '__main__':

    mq = Queue()

    mq.put("1")

    mq.put("2")

    mq.put("3")


    p_list = []

    for i in range(3):

        p = MyProcess(f"p{i}",mq)

        p_list.append(p)


    for p in p_list:

        p.start()

        #p.join()


为什么要加join,不然会报错,mac

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/spawn.py", line 116, in spawn_main

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/spawn.py", line 116, in spawn_main

        exitcode = _main(fd, parent_sentinel)exitcode = _main(fd, parent_sentinel)

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/spawn.py", line 126, in _main

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/spawn.py", line 126, in _main

    self = reduction.pickle.load(from_parent)

      File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/synchronize.py", line 110, in __setstate__

self = reduction.pickle.load(from_parent)

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/synchronize.py", line 110, in __setstate__

    self._semlock = _multiprocessing.SemLock._rebuild(*state)

FileNotFoundError: [Errno 2] No such file or directory

    self._semlock = _multiprocessing.SemLock._rebuild(*state)

FileNotFoundError: [Errno 2] No such file or directory

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/spawn.py", line 116, in spawn_main

    exitcode = _main(fd, parent_sentinel)

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/spawn.py", line 126, in _main

    self = reduction.pickle.load(from_parent)

  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/synchronize.py", line 110, in __setstate__

    self._semlock = _multiprocessing.SemLock._rebuild(*state)

FileNotFoundError: [Errno 2] No such file or directory


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 616楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 622楼

# 服务器端:一个线程专门发送消息,一个线程专门接收消息。
# 客户端:一个线程专门发送消息,一个线程专门接收消息。
# 结合多线程实现TCP双向传送(自由聊天)
# 【示例】TCP服务端结合多线程实现自由收发信息



from socket import *
from threading import Thread
def recv_data():
    while True:
        # 读取客户端的消息
        re_data = tcp_client_socket.recv(1024)
        re_content =re_data.decode('gbk')
        # 将消息输出到控制台
        print(f"客户端说:{re_content},from:{host}")
        if re_content == '88':
            break
        # 获取控制台信息
def send_data():
    while True:
        msg = input(">")
        tcp_client_socket.send(msg.encode("gbk"))

if __name__ == '__main__':
    tcp_server_socket = socket(AF_INET, SOCK_STREAM)
    tcp_server_socket.bind(('127.0.0.1', 8888))
    tcp_server_socket.listen()
    print("服务端已经启动,等待客户端连接!")
    tcp_client_socket, host = tcp_server_socket.accept()
    print("一个客户端建立连接成功!")

    t1 = Thread(target=recv_data())
    t2 = Thread(target=send_data())
    t1.start()
    t2.start()
    t1.join()
    t2.join()


tcp_client_socket.close()
tcp_server_socket.close()

from socket import *
from threading import Thread

def recv_data():
    while True:
        redata=tcp_client_socket.recv(1024)
        recv_data=redata.decode('gbk')
        print(f"服务器端说:{recv_data}")
def send_data():
    while True:
        msg=input(">")
        tcp_client_socket.send(msg.encode("gbk"))
        if msg == '88':
            break
if __name__ == '__main__':

    tcp_client_socket=socket(AF_INET,SOCK_STREAM)
    tcp_client_socket.connect(('127.0.0.1',8888))
    print("连接建立成功")
    t1=Thread(target=recv_data())
    t2=Thread(target=send_data())
    t1.start()
    t2.start()
    t1.join()
    t2.join()
tcp_client_socket.close()

这个持续通信中的发送消息接收信息判断为什么不生效,当输入88的时候


Python 全系列/第三阶段:Python 网络与并发编程/网络通信 629楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637