我正在寫一個簡單的消息程序(2個用戶),在終端上工作。這個多處理的python腳本有什麼問題?
對於它的實現,我決定創建2個進程,一個用於服務器(等待消息到達另一個用戶),另一個用於客戶端(只是將消息發送到另一個服務器進程用戶)
的事實是,當我運行它,我得到以下錯誤:
C:\>python message.py
> Process Process-2:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in_bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\message.py", line 12, in send_messages
message = raw_input('> ')
EOFError
Process Process-1:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in
_bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\message.py", line 25, in receive_messages
message = sc.recv(1024)
error: [Errno 10054] An existing connection was forcibly closed by the
remote host
這是我的Python代碼
from multiprocessing import Process
import socket
direction = "localhost"
global s
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def send_messages():
s.connect((direction, 5124))
while True:
message = raw_input('> ')
s.send(message)
if message == 'close':
break
print 'Bye'
s.close()
def receive_messages():
s.bind(("localhost",5124))
s.listen(2)
sc, addr = s.accept()
while True:
message = sc.recv(1024)
print message
sc.close()
s.close()
if __name__ == '__main__':
p1 = Process(target = receive_messages)
p1.start()
p2 = Process(target = send_messages)
p2.start()
p1.join()
p2.join()
注1:可以有由於從我的文本編輯器剪切並粘貼到計算器,會導致一些縮進錯誤。
注2:我在Windows 10