我正在尝试从服务器向客户端发送可调用函数。为了模拟这一点,我启动了两个进程或两个终端并运行以下代码 在服务器端
### THIS IS ON THE SERVER SIDE
from multiprocessing.connection import Listener
import threading
def start_thread(name, target, args=(), kwargs={}, daemon=True):
thread = threading.Thread(name=name, target=target, args=args, kwargs=kwargs)
thread.daemon = daemon
thread.start()
return thread
# client
def child(conn):
while True:
try:
msg = conn.recv()
# this just echos the value back, replace with your custom logic
conn.send(msg)
except EOFError as err:
break
except Exception as err:
print(err)
# server
def mother(address):
serv = Listener(address, authkey='mypassword')
while True:
client = serv.accept()
print(client, type(client))
t = start_thread('child', child, kwargs={'conn':client})
print(t)
start_thread('mother',mother, (('', 5000),))
在客户端,我正在运行此代码
## THIS IS ON THE CLIENT SIDE
from multiprocessing.connection import Client
c = Client(('localhost', 5000), authkey='mypassword')
c.send('hello')
print( c.recv())
c.send({'a': 123})
print(c.recv())
def func():
return 'i am a function'
c.send(func)
print(c.recv())
从客户端发送func之后,我在服务器终端中收到以下错误
'module' object has no attribute 'func'