会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 811楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 815楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 819楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 820楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 822楼

import multiprocessing
import time


def func1(conn1):
    sub_info="Hello!"
    print(f"进程1--{multiprocessing.current_process().pid} 发送参数{sub_info}")
    time.sleep(1)
    conn1.send(sub_info)
    print(f"来自线程2:{conn1.recv()}")
    time.sleep(1)

def func2(conn2):
    sub_info="你好!"
    print(f"进程2--{multiprocessing.current_process().pid} 发送参数{sub_info}")
    time.sleep(1)
    conn2.send(sub_info)
    print(f"来自线程1:{conn2.recv()}")
    time.sleep(1)



if __name__ == '__main__':
    #创建管道
    conn1,conn2=multiprocessing.Pipe()
    #创建子进程
    process1=multiprocessing.Process(target=func1,args=("conn1",))
    process2=multiprocessing.Process(target=func2,args=("func2",))
    #启动进程
    process1.start()
    process2.start()

进程2--59024发送参数你好!

进程1--51220发送参数Hello!

Process Process-2:

Process Process-1:

Traceback (most recent call last):

Traceback (most recent call last):

  File "D:\Anaconda\anaconda3\envs\python\lib\multiprocessing\process.py", line 315, in _bootstrap

    self.run()

  File "D:\Anaconda\anaconda3\envs\python\lib\multiprocessing\process.py", line 108, in run

    self._target(*self._args, **self._kwargs)

  File "D:\Pycharm\python学习\python系统化学习\网络与并发编程\Pipe实现进程间的通信.py", line 9, in func1

    conn1.send(sub_info)

  File "D:\Anaconda\anaconda3\envs\python\lib\multiprocessing\process.py", line 315, in _bootstrap

    self.run()

  File "D:\Anaconda\anaconda3\envs\python\lib\multiprocessing\process.py", line 108, in run

    self._target(*self._args, **self._kwargs)

AttributeError: 'str' object has no attribute 'send'

  File "D:\Pycharm\python学习\python系统化学习\网络与并发编程\Pipe实现进程间的通信.py", line 17, in func2

    conn2.send(sub_info)

AttributeError: 'str' object has no attribute 'send'

这个哪里错了呢

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

import threading
import time
from queue import Queue
class Pro(threading.Thread):
    def run(self):
        global qu
        count=0
        while True:
            if qu.qsize()<1000:
                for i in range(100):
                    count+=1
                    msg="生成产品"+str(count)
                    qu.put(msg)
                    print(msg)
            time.sleep(1)
class Con(threading.Thread):
    def run(self):
        global qu
        while True:
            if qu.qsize()>100:
                for i in range(3):
                    msg=self.name+"消费了"+qu.get()
                    print(msg)
            time.sleep(1)
if __name__=="__main__":
    qu=Queue
    for i in range(500):
        qu.put("初始产品"+str(i),)
    for i in range(2):
        p=Pro()
        p.start()
    for i in range(5):
        c=Con()
        c.start()

代码跟老师的一样,为什么会报这样的错??????????

Traceback (most recent call last):

  File "C:/Users/齐泉/PycharmProjects/1/test2.py", line 28, in <module>

    qu.put("初始产品"+str(i))

TypeError: put() missing 1 required positional argument: 'item'


Process finished with exit code 1


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 824楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 825楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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