Exemplo n.º 1
0
async def test_api_list_with_no_functions():
    server = FunctionServer(shutdown_mode='terminate')

    server._construct_endpoints()

    client = await aiohttp_client(server._app)

    res = await client.get('/function/list/data')
    assert res.status == 200
    assert await res.json() == []

    res = await client.get('/function/list/text')
    assert res.status == 200
    assert await res.text() == ''
Exemplo n.º 2
0
async def test_api_function_info():
    def test1():
        pass

    def test2():
        pass

    server = FunctionServer(shutdown_mode='terminate')
    server.add_function(test1, [], 1)
    server.add_function(test2, [ArgDefinition('x', ArgType.INTEGER, True, 'value x')], 2)

    server._construct_endpoints()

    client = await aiohttp_client(server._app)

    res = client.get('/api/function/definition/test1')
    assert res.status == 200

    data = await res.json()
    assert data['name'] == 'test1'
    assert len(data['arg_definitions']) == 0
    assert data['max_concurrency'] == 1

    res = await client.get('/api/function/definition/test2')
    assert res.status == 200

    data = await res.json()
    assert data['name'] == 'test2'
    assert len(data['arg_definitions']) == 1
    assert data['max_concurrency'] == 2

    res = client.get('/api/function/running_count/test1')
    assert res.status == 200
    assert await res.json() == 0

    res = client.get('/api/function/running_count/test2')
    assert res.status == 200
    assert await res.json() == 0

    res = client.get('/api/function/definition/test3')
    assert res.status == 404

    res = client.get('/api/function/running_count/test3')
    assert res.status == 404
Exemplo n.º 3
0
async def test_api_list_function_with_different_endpoint():
    def function():
        pass

    server = FunctionServer(shutdown_mode='terminate')
    server.add_function(function, [], 1)
    server.add_function(function, [], 1, '', 'function_another_name')

    server._construct_endpoints()

    client = await aiohttp_client(server._app)

    res = await client.get('/api/list/data')
    assert res.status == 200
    assert await res.json() == ['function', 'function_another_name']

    res = await client.get('/api/list/text')
    assert res.status == 200
    assert await res.text() != ''
def server_process_for_test_task_status():
    tss = TaskStoreSettings()
    tss.sqlite_dsn = os.path.join(gettempdir(),
                                  'server_process_for_test_task_status.db')

    server = FunctionServer(shutdown_mode='terminate',
                            port=8889,
                            task_store_settings=tss)
    server.add_function(status_check_func, [], 1)
    server.add_function(status_check_func_fail, [], 1)

    def terminate(sig, handler):
        try:
            # FIXME: don't stop immediately
            server._app.stop()
            server.exit_with_terminate()
        except Exception:
            pass

    server.start()
def sleep_function_server_process():
    tss = TaskStoreSettings()
    tss.sqlite_dsn = os.path.join(gettempdir(),
                                  'sleep_function_server_process.db')

    server = FunctionServer(shutdown_mode='terminate',
                            register_sys_signals=True,
                            task_store_settings=tss)
    server.add_function(
        sleep_func, [ArgDefinition('t', ArgType.INTEGER, True, 'Sleep Time')],
        10)

    def terminate(sig, handler):
        try:
            # FIXME: don't stop immediately
            server._app.stop()
            server.exit_with_terminate()
        except Exception:
            pass

    server.start()
Exemplo n.º 6
0
async def test_api_list_with_functions():
    def test1():
        pass

    def test2():
        pass

    server = FunctionServer(shutdown_mode='terminate')
    server.add_function(test1, [], 1)
    server.add_function(test2, [], 1)

    server._construct_endpoints()

    client = await aiohttp_client(server._app)

    res = await client.get('/api/list/data')
    assert res.status == 200
    assert await res.json() == ['test1', 'test2']

    res = await client.get('/api/list/text')
    assert res.status == 200
    assert await res.text() != ''
Exemplo n.º 7
0
import sys

# for develpment
try:
    import restful_functions  # NOQA
except Exception:
    print('using local version')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# ------------ #


from restful_functions import ArgDefinition, ArgType, FunctionServer


def addition(x: int, y: int):
    print(os.getpid(), os.getppid(), f'{x}+{y}={x+y}')
    return x+y


if __name__ == '__main__':
    server = FunctionServer(debug=True)
    server.add_function(
        addition,  # Function
        [
            ArgDefinition('x', ArgType.INTEGER, True, 'x'),
            ArgDefinition('y', ArgType.INTEGER, True, 'y'),
        ],  # Args
        1,  # Max Concurrency
        '足し算')  # Description
    server.start()
        112272535095293,
        112582705942171,
        112272535095293,
        115280095190773,
        115797848077099,
        1099726899285419]

    with concurrent.futures.ProcessPoolExecutor() as executor:
        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
            print('%d is prime: %s' % (number, prime))

    return True


if __name__ == '__main__':
    server = FunctionServer(debug=True)

    server.add_function(
        addition,
        [
            ArgDefinition('x', ArgType.INTEGER, True, 'x'),
            ArgDefinition('y', ArgType.INTEGER, True, 'y'),
        ],
        10,
        'Simple Addition')

    server.add_function(no_arg_job, [], 1)

    server.add_function(multi, [], 2, 'Multi Process Function')

    # Differenct Endopint for same function. Max concurrency is checked indivisualy.
# for develpment
try:
    import restful_functions  # NOQA
except Exception:
    print('using local version')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# ------------ #


import time

from restful_functions import FunctionServer


def long_process():
    time.sleep(100)


if __name__ == '__main__':
    # Make polling_timeout_process_interval a smaller value than defaults to confirm a terminated process due to timeout
    server = FunctionServer(polling_timeout_process_interval=1.0, debug=True)
    server.add_function(
        long_process,
        [],
        1,  # concurency
        'timeout test',
        None,
        10  # timeout
    )
    server.start()