如何在两个python脚本中共享套接字变量
创建套接字绑定的文件' zmq_settings.py'
import zmq
def create_socket_connection():
zmq_port = "XXX"
context = zmq.Context()
global zmq_socket
zmq_socket = context.socket(zmq.REP)
zmq_socket.bind("tcp://*:%s" % zmq_port)
def client_send_json(send_json):
return zmq_socket.send_json(send_json)
接受来自客户端&server;'
的请求的服务器文件import zmq_settings
create_socket_connection()
while True:
try:
print "\n************\t****************"
print "[radio_server]\t Waiting..."
incoming_json = zmq_settings.client_recv_json() # Wait for next request from client
print "Received request: ", incoming_json
zmq_settings.client_send_json(json_success)
还有另一个文件需要使用套接字变量发送到同一个客户端' event_listener.py'
import zmq_settings
if CONDITION
send_event_to_client(event_json)
else:
print "jfjd"
在函数内部使用全局关键字不起作用,并且初始化" zmq_settings =无'在函数" create_socket_connection"之上给出错误