tuobameng tuobameng - 6 months ago 76
Linux Question

TimeoutError when two computers comunicated

I am using Python3 to learn distributed programming

there are two python fileļ¼Œone's name is main.py, it distributes information, the other one manipulation data, and the name is worker.py.

everything goes well when I run this two file in one computer[set server address = 127.0.0.1, port = 5000]

but when i run these two files in seperate computers, they cannot connect to each other, and TimeoutError was encoutered.

I don't know why. one computer is Win10 at my home, the other is a linux cloud server which I baught.

the code works in one computer. but when I ran main.py in linux, and ran worker.py{change server to linux's ip address} in win10, then the worker.py encounter a TimeoutError

I know nothing about the linux, is there some security settings I need to open or close?

"""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')

Answer Source

The address 127.0.0.1 very specifically refers to the same computer where the code is running.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download