Example #1
0
 def test_tunnuel(self): 
     def test_func(i): 
         print '#%s, pid=%d'%(i, os.getpid())
         time.sleep(1)
         return 3
     
     #Process(target=queue, args=(func, )).start() 
     for i in range(5): 
         print_vars(vars(),  ['i'])
         #print queue(test_func, args=(i, ), querry=1, tunnel=0, port=9090, tunnel_server='localhost')
         #print queue(test_func, args=(i, ), querry=0, tunnel=1, port=9090, tunnel_server='[email protected]:22')
         print queue(test_func, args=(i, ), querry=1, 
                 tunnel=1, host='node93', port=9090, 
                 querry_timeout= 0.1, 
                 tunnel_server= '[email protected]')
Example #2
0
def conn_server(server, info=0): 
    """
        return the result of running the task *runnable* with the given 
        arguments.
        
        params: 
            host: e.g. '210.45.117.30' or 'qtg7501' if use the later should add ip hostname pair in /etc/hosts
            querry:  querry whether server available 
            querry_timeout: 
                我曾经试过用 stopit module 来给recv设置timeout, 但是没有成功,应该是涉及到背后线程没有关闭
                refer to https://github.com/zeromq/pyzmq/issues/132
    """
    server_info = resolve_server(server)
    host = server_info['host']
    port = server_info['port']
    tunnel = server_info['tunnel']
    tunnel_server = server_info['tunnel_server']
    
    if info>0: 
        print_vars(vars(),  ['server_info'])
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    url = 'tcp://{}:{}'.format(host, port)
    if not tunnel:  #one should either use connect or tunnel_connection, not both.
        socket.connect(url)
    else:
        #zmq.ssh.tunnel_connection(socket, url, "myuser@remote-server-ip")
        #issue:  似乎tunnel对port有限制,不能用90900这样5位数的的端口
        zmq.ssh.tunnel_connection(socket, url, tunnel_server)
        #print 'tunnel succeed: {}'.format(url)
    
    if 0: 
        socket.setsockopt(zmq.LINGER, 0)   #this is needed or else timeout wont work 
        #socket.send_pyobj('querry')    
        socket.send_pyobj({'header': 'querry'})    
        # use poll for timeouts:
        poller = zmq.Poller()
        poller.register(socket, zmq.POLLIN)
        if poller.poll(querry_timeout*1000): # 10s timeout in milliseconds
            server_status = socket.recv_pyobj()
        else:
            #raise IOError("Timeout processing auth request")        
            status = server_status = 'not_reachable'

        if 0:  # below not working  
            try: 
                with stopit.SignalTimeout(querry_timeout, False) as ctx:
                #with stopit.ThreadingTimeout(querry_timeout, False) as ctx:
                    print 'tttttry', port, host  
                    server_status = socket.recv_pyobj()
            except Exception as err: 
                print 'rrrraise', err 
                #socket.close()
                #context.term()
                #raise 
                #server_status = 'not_reachable'
                raise 
            print 'sssssss', ctx.state    
            if ctx.state == ctx.EXECUTED:
                pass # All's fine, everything was executed within 10 seconds
            elif ctx.state == ctx.EXECUTING:
                pass # Hmm, that's not possible outside the block
            elif ctx.state == ctx.TIMED_OUT:
                server_status = 'not recheable' # Eeek the 10 seconds timeout occurred while executing the block
            elif ctx.state == ctx.INTERRUPTED:
                pass 
                # Oh you raised specifically the TimeoutException in the block
            elif ctx.state == ctx.CANCELED:
                pass # Oh you called to_ctx_mgr.cancel() method within the block but it # executed till the end
            else:
                pass 
                # That's not possible            
            #print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.state == ctx.EXCUTING 
            print 'aaaaaafter ', ctx.state == ctx.TIMED_OUT , ctx.TIMED_OUT,  
    
    # these are not necessary, but still good practice:
    #socket.close()
    #context.term()    
    return context, socket