利用python中的mangers和queue模块做一个两个电脑通信的demo,却说没有register这个属性 5C

talkDemo_1

import queue,time
from multiprocessing.managers import BaseManager
from datetime import datetime

message1 = queue.Queue()
message2 = queue.Queue()

class QueueManager(BaseManager):
    pass

def get_message1():
    global message1
    return message1

def get_message2():
    global message2
    return message2

QueueManager.register('message_queue_1',callable = get_message1)
QueueManager.register('message_queue_2',callable = get_message2)

def startManager(host,port,key):
    manager = QueueManager(address = (host,port),authkey = key)
    manager.start()
    return manager


if __name__ == '__main__':
    manager = startManager('127.0.0.1', 5000, b'abc')
    mess1 = manager.message_queue_1()
    mess2 = manager.message_queue_2()
    #mess1.put(1)
    #mess2.get()
    name = input('请输入您的昵称:')
    print('可以开始通信了!')
    while(True):
        inform = input()
        if inform == 'over':
            mess1.put(inform)
            time.sleep(5)
            break
        inform = '%s\n\t%s:%s' % (datetime.now(),name,inform)
        mess1.put(inform)
        print(inform)
        getInform = mess2.get()
        print(getInform)
    manager.shutdown()
    print('结束通话')

talkDemo_2.py

import queue
from multiprocessing.managers import BaseManager
from datetime import datetime

def QueueManager(BaseManager):
    pass

QueueManager.register('message_queue_1')
QueueManager.register('message_queue_2')    

def connectManager(addr,port,key):
    manager = QueueManager(address = (addr, port), authkey = key)
    manager.connect()
    return manager

if __name__ == '__main__':
    addr = '127.0.0.1'
    print('connect to %s...' % addr)
    manager = connectManager(addr,5000,b'abc')
    mess1 = manager.message_queue_1()
    mess2 = manager.message_queue_2()
    #mess1.get()
    #mess2.put(1)
    name = input('请输入您的昵称:')
    print('已连接,可以开始通信了!')
    while(True):
        getInform = mess1.get()
        print(getInform)
        if getInform == 'over':
            break
        inform = input()
        inform = '%s\n\t%s:%s' % (datetime.now(),name,inform)
        print(inform)
        mess2.put(inform)
    print('通话结束')

运行talkDemo_2.py的时候,报错Traceback (most recent call last):
File "E:\Python_program\practice\talkDemo_2.py", line 8, in
QueueManager.register('message_queue_1')
AttributeError: 'function' object has no attribute 'register'

1个回答

weixin_41908053
liuxin95 Basemanage出问题了,queue没报错
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐