15
15
16
16
import copy
17
17
import multiprocessing
18
+ import os
18
19
import random
20
+ import tempfile
19
21
import threading
20
22
import time
21
23
import types
24
+ import uuid
22
25
from contextlib import ExitStack
23
26
from enum import Enum
24
27
from typing import Tuple , List , Union , Type
@@ -113,8 +116,19 @@ class EventLoopEnd(Exception):
113
116
pass
114
117
115
118
116
- def build_socket (ctx : 'zmq.Context' , host : str , port : int , socket_type : 'SocketType' , identity : 'str' = None ) -> Tuple [
117
- 'zmq.Socket' , str ]:
119
+ def get_random_ipc () -> str :
120
+ try :
121
+ tmp = os .environ ['GNES_IPC_SOCK_TMP' ]
122
+ if not os .path .exists (tmp ):
123
+ raise ValueError ('This directory for sockets ({}) does not seems to exist.' .format (tmp ))
124
+ tmp = os .path .join (tmp , str (uuid .uuid1 ())[:8 ])
125
+ except KeyError :
126
+ tmp = tempfile .NamedTemporaryFile ().name
127
+ return 'ipc://%s' % tmp
128
+
129
+
130
+ def build_socket (ctx : 'zmq.Context' , host : str , port : int ,
131
+ socket_type : 'SocketType' , identity : 'str' = None , use_ipc : bool = False ) -> Tuple ['zmq.Socket' , str ]:
118
132
sock = {
119
133
SocketType .PULL_BIND : lambda : ctx .socket (zmq .PULL ),
120
134
SocketType .PULL_CONNECT : lambda : ctx .socket (zmq .PULL ),
@@ -129,11 +143,14 @@ def build_socket(ctx: 'zmq.Context', host: str, port: int, socket_type: 'SocketT
129
143
}[socket_type ]()
130
144
131
145
if socket_type .is_bind :
132
- host = BaseService .default_host
133
- if port is None :
134
- sock .bind_to_random_port ('tcp://%s' % host )
146
+ if use_ipc :
147
+ sock .bind (host )
135
148
else :
136
- sock .bind ('tcp://%s:%d' % (host , port ))
149
+ host = BaseService .default_host
150
+ if port is None :
151
+ sock .bind_to_random_port ('tcp://%s' % host )
152
+ else :
153
+ sock .bind ('tcp://%s:%d' % (host , port ))
137
154
else :
138
155
if port is None :
139
156
sock .connect (host )
@@ -329,8 +346,12 @@ def __init__(self, args):
329
346
self .last_dump_time = time .perf_counter ()
330
347
self ._model = None
331
348
self .use_event_loop = True
332
- self .ctrl_addr = 'tcp://%s:%d' % (self .default_host , self .args .port_ctrl )
333
- self .logger .info ('control address: %s' % self .ctrl_addr )
349
+ self .ctrl_with_ipc = (os .name != 'nt' ) and self .args .ctrl_with_ipc
350
+ if self .ctrl_with_ipc :
351
+ self .ctrl_addr = get_random_ipc ()
352
+ else :
353
+ self .ctrl_addr = 'tcp://%s:%d' % (self .default_host , self .args .port_ctrl )
354
+
334
355
self .send_recv_kwargs = dict (
335
356
check_version = self .args .check_version ,
336
357
timeout = self .args .timeout ,
@@ -401,7 +422,12 @@ def _run(self, ctx):
401
422
self .handler .service_context = self
402
423
# print('!!!! t_id: %d service_context: %r' % (threading.get_ident(), self.handler.service_context))
403
424
self .logger .info ('bind sockets...' )
404
- ctrl_sock , ctrl_addr = build_socket (ctx , self .default_host , self .args .port_ctrl , SocketType .PAIR_BIND )
425
+ if self .ctrl_with_ipc :
426
+ ctrl_sock , ctrl_addr = build_socket (ctx , self .ctrl_addr , None , SocketType .PAIR_BIND ,
427
+ use_ipc = self .ctrl_with_ipc )
428
+ else :
429
+ ctrl_sock , ctrl_addr = build_socket (ctx , self .default_host , self .args .port_ctrl , SocketType .PAIR_BIND )
430
+
405
431
self .logger .info ('control over %s' % (colored (ctrl_addr , 'yellow' )))
406
432
407
433
in_sock , _ = build_socket (ctx , self .args .host_in , self .args .port_in , self .args .socket_in ,
@@ -412,7 +438,6 @@ def _run(self, ctx):
412
438
self .args .identity )
413
439
self .logger .info ('output %s:%s' % (self .args .host_out , colored (self .args .port_out , 'yellow' )))
414
440
415
-
416
441
self .logger .info (
417
442
'input %s:%s\t output %s:%s\t control over %s' % (
418
443
self .args .host_in , colored (self .args .port_in , 'yellow' ),
0 commit comments