async def test_executor_context_init(): from pymoa_remote.threading import ThreadExecutor from pymoa_remote.tests.device import RandomDigitalChannel tid = get_ident() device = RandomDigitalChannel() async with ThreadExecutor(init_context=False) as executor: async with executor.remote_instance(device, 'some device'): assert await device.get_thread_ident() == tid with ExecutorContext(executor): executor_tid = await device.get_thread_ident() assert executor_tid != tid async with ThreadExecutor() as executor: async with executor.remote_instance(device, 'some device'): executor_tid = await device.get_thread_ident() assert executor_tid != tid async with ThreadExecutor() as executor2: async with executor2.remote_instance(device, 'some device'): executor_tid2 = await device.get_thread_ident() assert executor_tid != tid assert executor_tid2 != executor_tid assert executor_tid2 != tid with ExecutorContext(executor): executor_tid_ = await device.get_thread_ident() assert executor_tid_ == executor_tid
async def device(request, quart_socket_executor, quart_rest_executor): from pymoa_remote.tests.device import RandomDigitalChannel with ExecutorContext(locals()[request.param]) as context: device = RandomDigitalChannel() async with context.executor.remote_instance(device, 'rand_device'): yield device
async def quart_rest_executor(quart_app): from pymoa_remote.rest.client import RestExecutor from pymoa_remote.client import ExecutorContext async with RestExecutor( uri=f'http://127.0.0.1:{quart_app.pymoa_port}') as executor: with ExecutorContext(executor): yield executor
async def process_executor(): from pymoa_remote.socket.multiprocessing_client import \ MultiprocessSocketExecutor from pymoa_remote.client import ExecutorContext async with MultiprocessSocketExecutor( server='127.0.0.1', allow_import_from_main=True) as executor: with ExecutorContext(executor): yield executor
async def quart_socket_executor(quart_app, nursery): from pymoa_remote.socket.websocket_client import WebSocketExecutor from pymoa_remote.client import ExecutorContext async with WebSocketExecutor( nursery=nursery, server='127.0.0.1', port=quart_app.pymoa_port) as executor: with ExecutorContext(executor): yield executor
async def start_app(app, host='127.0.0.1', port=5000, task_status=trio.TASK_STATUS_IGNORED): async def before_first(): task_status.started() # start/stop thread executor app.before_serving(before_first) executor = app.rest_executor.executor async with executor: with ExecutorContext(executor): await app.run_task(host, port)
async def serve(host, port, stream_changes, allow_remote_class_registration, allow_import_from_main, max_queue_size): # todo: catch and send back errors (ignoring socket closing errors?) global MAX_QUEUE_SIZE MAX_QUEUE_SIZE = max_queue_size thread_executor = ThreadExecutor() executor = ProcessSocketServer(executor=thread_executor) executor.stream_changes = stream_changes executor.allow_remote_class_registration = allow_remote_class_registration executor.allow_import_from_main = allow_import_from_main sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (host, port) await sock.bind(server_address) sock.listen() async def handler(stream: SocketStream): data = await executor.read_decode_json_buffers(stream) if data.get('eof', False): # todo: close in a nicer way by not accepting new requests nursery.cancel_scope.cancel() return channel = data.get('stream', None) try: if channel is None: await socket_handler(executor, stream) elif channel == 'data': await socket_data_stream_handler(executor, stream, data['data']) else: await socket_stream_handler(executor, stream, channel, data['data']) except BrokenResourceError: # if channel is None, all errors are caught and sent to client. # Otherwise, and errors when channel != None are a client socket # issue so we can just drop it pass with ExecutorContext(thread_executor): async with thread_executor: async with trio.open_nursery() as nursery: nursery.start_soon(serve_listeners, handler, [SocketListener(sock)])
async def remote_executor(request, process_executor, quart_socket_executor, quart_rest_executor): with ExecutorContext(locals()[request.param]) as context: yield context.executor
async def thread_executor(): from pymoa_remote.threading import ThreadExecutor from pymoa_remote.client import ExecutorContext async with ThreadExecutor() as executor: with ExecutorContext(executor): yield executor