Example #1
0
def start():
    """
    Start the client application by allocating a logger, parsing args and then forwarding the args on to the client
    """
    basicConfig(format='%(levelname)s [%(asctime)s]: %(message)s', level=DEBUG)
    logger = getLogger()
    try:
        try:
            args = get_args()

            if not args.verbose:
                logger.setLevel(CRITICAL)

            ui = Interface()
            if not args.username:
                args.username = ui.request_username()

            if not args.password:
                args.password = ui.request_pass()

            # Create a client
            s = Client(args.server_addr, args.port, args.username,
                       args.password, ui, logger)

            # Create an event loop
            loop = Loop()
            # Bind Client to event Loop
            s.start(loop)
        except KeyboardInterrupt:
            s_exit(1)
        else:
            # Run the event loop
            loop.run()
            s_exit(0)
    except ArgumentError as e:
        print("Argument Error: {}".format(e))
        s_exit(1)
Example #2
0
# Idle handles will run the given callback once per loop iteration,
# right before the Prepare handles.
#
# Warning Despite the name, Idle handles will get their callbacks
# called on every loop iteration, not when the loop is actually "idle".

from pyuv import Loop
from pyuv import Idle

counter = 0


def idle_callback(handle):
    global counter
    counter += 1
    if counter >= 100000:
        handle.stop()


loop = Loop.default_loop()

myidle = Idle(loop)
myidle.start(idle_callback)

print 'Idling...'
loop.run()

print 'counter =', counter
Example #3
0
import time
from pyuv import Loop
from pyuv import Timer

counter = 0
def timer_callback(handle):
	global counter
	counter += 1
	if counter < 10:
		print 'counter = %d, %s' % (counter, time.strftime('%H:%M:%S'))
	else:
		handle.stop()

loop = Loop.default_loop()

handle = Timer(loop)
handle.start(timer_callback, 3.5, 2) # timeout = 3.5s, repeat = 2s

print 'timer start,', time.strftime('%H:%M:%S')
loop.run()

Example #4
0
 def setUp(self):
     super(CustomAppMixin, self).setUp()
     loop = self.loop = Loop()
     self.app = ThriftWorker(loop=loop)
Example #5
0
 def loop(self):
     """Create event loop. Should be running in separate thread."""
     return Loop()
Example #6
0
 def __init__(self):
     self.pipe = Pipe(Loop.default_loop())
Example #7
0
from pyuv import Loop
from pyuv import UV_RUN_DEFAULT, UV_RUN_ONCE, UV_RUN_NOWAIT

loop = Loop()

print "Now quitting."
loop.run(UV_RUN_DEFAULT)

Example #8
0
 def test_custom_loop(self):
     custom_loop = Loop()
     app = ThriftWorker(loop=custom_loop)
     self.assertIs(custom_loop, app.loop)
Example #9
0
 def loop(self):
     return Loop.default_loop()
def start():
    """
    Start the server application by parsing the args
    """
    basicConfig(format='%(levelname)s [%(asctime)s]: %(message)s', level=DEBUG)
    logger = getLogger()
    loop = Loop()
    try:
        # Parse args
        args = get_args()

        # Set the log level if not in verbose mode
        if not args.verbose:
            logger.setLevel(INFO)
        if args.quiet:
            logger.setLevel(WARNING)

        # Allocate a database and register some test users
        db = DictionaryDB(logger)
        db.register_user(b"cam", b"mac", 1200)
        db.register_user(b"jen", b"nej", 1200)
        db.register_user(b"kain", b"niak", 1200)
        db.register_user(b"andrei", b"ierdna", 1200)
        db.register_user(b"neil", b"lien", 1200)
        db.register_user(b"safa", b"afas", 1200)
        db.register_user(b"colbert", b"trebloc", 1200)
        db.register_user(b"khanh", b"hnahk", 1200)

        # Allocate a Queue
        queue = UserQueue(logger)

        # Create Session Callback
        def create_session(socket: TCP):
            """
            Wrapper for the Session constructor that defaults the queue and db parameters
            :param socket: TCP connection to create the session with
            :return: A new session
            """
            return Session(socket, queue, db, logger)

        # Allocate a Server
        s = Server(args.listen_addr, args.listen_port, create_session, logger)

        # Allocate an advertiser
        a = Advertiser(args.listen_addr, args.broadcast_addr, args.listen_port,
                       args.udp_port, logger)

        # Allocate a Signal Handler
        sig = Signal(loop)

        def on_signal(sig_handler: Signal, signal: int):
            """
            On SIGINT, stop all things depending on the loop and close the loop
            :param sig_handler: Signal handler that caught the signal
            :param signal: The signal that was received
            """
            logger.info(
                "Caught signal {signal}, shutting down".format(signal=signal))
            sig_handler.stop()
            queue.stop()
            a.stop()
            s.close()
            loop.stop()

        # Bind the objects to the event loop
        sig.start(on_signal, SIGINT)
        s.start(loop)
        a.start(loop)
        queue.register_loop(loop)

        # Run the event Loop
        loop.run()
        s_exit(0)
    except ArgumentError:
        logger.error("Argument Error: ", exc_info=True)
        s_exit(1)