<commit_before>#!/usr/bin/env python

import asyncio as _asyncio
import json as _json
import traceback as _tb
from datetime import datetime as _dt
import websockets as _websockets


def _run(local, remote):
    async def _func(websocket, path):
        msg = await websocket.recv()
        try:
            msg = _json.loads(msg)

        except Exception as e:
            await websocket.close(1011, f'JSON data parse failed with {msg}')

        if msg['mode'] == 'remote':
            try:
                start = _dt.now()
                output = await _asyncio.get_event_loop().run_in_executor(None, remote, msg['data'])
                print('Remote exec time:', (_dt.now() - start).total_seconds())
                await websocket.send(_json.dumps({'type': 'stdout', 'data': output, 'end': True}))

            except Exception as e:
                print('### Remote data: ', msg['data'])
                _tb.print_exc()
                await websocket.send(_json.dumps({'type': 'stderr', 'data': str(e), 'end': True}))

        elif msg['mode'] == 'local':
            try:
                start = _dt.now()
                output = await _asyncio.get_event_loop().run_in_executor(None, local, msg['data'])
                print('Local exec time:', (_dt.now() - start).total_seconds())
                await websocket.send(_json.dumps({'type': 'stdout', 'data': output, 'end': True}))

            except Exception as e:
                print('### Local data: ', msg['data'])
                _tb.print_exc()
                await websocket.send(_json.dumps({'type': 'stderr', 'data': str(e), 'end': True}))

        else:
            await websocket.close()

    return _func


def start(localFunction, remoteFunction):
    start_server = _websockets.serve(_run(localFunction, remoteFunction), '0.0.0.0', 8881)
    print("Python microservice started on 8881")

    _asyncio.get_event_loop().run_until_complete(start_server)
    _asyncio.get_event_loop().run_forever()<commit_msg>Added traceback for try:except and some refactoring.<commit_after>#!/usr/bin/env python

import asyncio as _asyncio
import json as _json
import traceback as _tb
from datetime import datetime as _dt
import websockets as _websockets


def _run(local, remote):
    async def _func(websocket, path):
        msg = await websocket.recv()
        try:
            msg = _json.loads(msg)

        except Exception as e:
            await websocket.close(1011, f'JSON data parse failed with {msg}')

        if msg['mode'] == 'remote':
            try:
                start = _dt.now()
                output = await _asyncio.get_event_loop().run_in_executor(None, remote, msg['data'])
                print('Remote exec time:', (_dt.now() - start).total_seconds())
                await websocket.send(_json.dumps({'type': 'stdout', 'data': output, 'end': True}))

            except Exception as e:
                print('### Remote data: ', msg['data'])
                _tb.print_exc()
                await websocket.send(_json.dumps({'type': 'stderr', 'data': str(e), 'end': True}))

        elif msg['mode'] == 'local':
            try:
                start = _dt.now()
                output = await _asyncio.get_event_loop().run_in_executor(None, local, msg['data'])
                print('Local exec time:', (_dt.now() - start).total_seconds())
                await websocket.send(_json.dumps({'type': 'stdout', 'data': output, 'end': True}))

            except Exception as e:
                print('### Local data: ', msg['data'])
                _tb.print_exc()
                await websocket.send(_json.dumps({'type': 'stderr', 'data': str(e), 'end': True}))

        else:
            await websocket.close()

    return _func


def start(localFunction, remoteFunction):
    start_server = _websockets.serve(_run(localFunction, remoteFunction), '0.0.0.0', 8881)
    print("Python microservice started on 8881")
    _asyncio.get_event_loop().run_until_complete(start_server)
    _asyncio.get_event_loop().run_forever()
{'fraction_non_alphanumeric': 0.13887506222000995, 'fraction_numerical': 0.007964161274265804, 'mean_word_length': 2.391561181434599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1087687', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1163, 'n_words': 299}