def lock(resources, *args, **kwargs):
    """ Lock resources from the command line, for example for maintenance. """
    # all resources are locked if nothing is specified
    if not resources:
        client = redis.Redis(decode_responses=True, **kwargs)
        resources = find_resources(client)

    if not resources:
        return

    # create one process per pid
    locker = Locker(**kwargs)
    while len(resources) > 1:
        pid = os.fork()
        resources = resources[:1] if pid else resources[1:]

    # at this point there is only one resource - lock it down
    resource = resources[0]
    try:
        print('{}: acquiring'.format(resource))
        with locker.lock(resource, label='lock tool'):
            print('{}: locked'.format(resource))
            try:
                signal.pause()
            except KeyboardInterrupt:
                print('{}: released'.format(resource))
    except KeyboardInterrupt:
        print('{}: canceled'.format(resource))
def lock ( resources , * args , ** kwargs ) : if not resources : client = redis . Redis ( decode_responses = True , ** kwargs ) resources = find_resources ( client ) if not resources : return locker = Locker ( ** kwargs ) while len ( resources ) > 1 : pid = os . fork ( ) resources = resources [ : 1 ] if pid else resources [ 1 : ] resource = resources [ 0 ] try : print ( '{}: acquiring' . format ( resource ) ) with locker . lock ( resource , label = 'lock tool' ) : print ( '{}: locked' . format ( resource ) ) try : signal . pause ( ) except KeyboardInterrupt : print ( '{}: released' . format ( resource ) ) except KeyboardInterrupt : print ( '{}: canceled' . format ( resource ) )
Lock resources from the command line, for example for maintenance.