gpt4 book ai didi

python - 两台电脑通信时出现TimeoutError

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:39 25 4
gpt4 key购买 nike

我正在使用Python3学习分布式编程

有两个python文件,一个是main.py,分发信息,一个是操作数据,worker.py。

当我在一台计算机上运行这两个文件时一切顺利[设置服务器地址= 127.0.0.1,端口= 5000]

但是当我在不同的计算机上运行这两个文件时,它们无法相互连接,并且遇到了 TimeoutError。

我不知道为什么。一台电脑是我家的Win10,一台是我买的linux云服务器。

代码在一台计算机上运行。但是当我在 linux 中运行 main.py,并在 win10 中运行 worker.py{change server to linux's ip address} 时,worker.py 遇到 TimeoutError

我对 linux 一无所知,是否需要打开或关闭一些安全设置?

"""main.py"""

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

TASK_QUEUE = queue.Queue()
RESULT_QUEUE = queue.Queue()

def get_task_queue():
"""set TASK_QUEUE as a function"""
global TASK_QUEUE
return TASK_QUEUE


def receive_result_queue():
"""set RESULT_QUEUE as a function"""
global RESULT_QUEUE
return RESULT_QUEUE


class QueueManager(BaseManager):
"""inherit BaseManager from multiprocessing.managers"""
pass

if __name__ == '__main__':
QueueManager.register('distribute_task_queue', callable=get_task_queue)
QueueManager.register('receive_result_queue', callable=receive_result_queue)

# bind port 5000, set verification code = 'abc'
MANAGER = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc')

# start manager
MANAGER.start()

TASK = MANAGER.distribute_task_queue()
RESULT = MANAGER.receive_result_queue()

# put each line into manager`enter code here`
with open("C:/Users/dayia/Desktop/log.20170817") as f:
for line in f:
TASK.put(line)

# try receive result
while 1:
try:
r = RESULT.get(timeout=1)
if r[0] == r[1] and r[0] == "done":
break
else:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),"line %s\'s length is %s" % (r[0], r[1]))
except queue.Empty:
print('result queue is empty.')
#
"""worker.py"""

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


class QueueManager(BaseManager):
"""inherit BaseManager from multiprocessing.managers"""
pass


QueueManager.register('distribute_task_queue')
QueueManager.register('receive_result_queue')

server_addr = '127.0.0.1'
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'Connect to server %s...' % server_addr)
m = QueueManager(address=(server_addr, 5000), authkey=b'abc')

m.connect()

TASK = m.distribute_task_queue()
RESULT = m.receive_result_queue()

def parse_line(line):
return len(line)

C = 0

while not TASK.empty():
try:
n = TASK.get(timeout=1)
r = parse_line(n)
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'running line %s, length is %s' % (C+1, r))
C += 1
RESULT.put([r, C])
except queue.Empty:
print('task queue is empty.')

RESULT.put(["done", "done"])
enter code here
print('worker exit')

最佳答案

地址 127.0.0.1 非常具体地指代码运行的同一台计算机(在网络术语中:127.0.0.1 的 IP 地址本地主机) .

关于python - 两台电脑通信时出现TimeoutError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47468045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com