Skip to content

Commit

Permalink
server2: rework shell
Browse files Browse the repository at this point in the history
This patch fixes multiple errors when running server2 in shell mode

Signed-off-by: Florian Scherf <f.scherf@pengutronix.de>
  • Loading branch information
fscherf committed Jan 9, 2020
1 parent c389d12 commit 695341d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
12 changes: 12 additions & 0 deletions bin/_flamingo-server2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-

import logging
import signal
import os

from flamingo.server2.logging import RPCHandler

Expand All @@ -23,6 +25,8 @@ parser = gen_default_parser(prog='flamingo-server2')

parser.add_argument('--port', type=int, default=8080)
parser.add_argument('--host', type=str, default='localhost')
parser.add_argument('--shell', action='store_true')
parser.add_argument('--shell-history', action='store_true')

namespace, settings = parse_args(parser, setup_logging=False)

Expand Down Expand Up @@ -51,6 +55,14 @@ try:
except KeyboardInterrupt:
exit()

# setup shell
if namespace.shell:
def start_shell(server):
server.start_shell(history=namespace.shell_history)
os.kill(os.getpid(), signal.SIGTERM)

loop.create_task(server.rpc.worker_pool.run(start_shell, server))

# run
print('starting server on http://localhost:{}/'.format(namespace.port))

Expand Down
32 changes: 25 additions & 7 deletions flamingo/server2/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

try:
import IPython
from traitlets.config import Config

IPYTHON = True

Expand All @@ -41,6 +42,7 @@ def __init__(self, app, settings, rpc_logging_handler, loop=None,
self.logger = logger

# locks
self._shell_running = False
self._locked = False
self._pending_locks = []

Expand Down Expand Up @@ -247,15 +249,31 @@ def get_meta_data(self, request, url):

return meta_data

def start_shell(self, request):
context = self.context # NOQA
history = self.history # NOQA
def start_shell(self, request=None, history=False):
if self._shell_running:
return

self._shell_running = True

try:
if IPYTHON:
config = Config()

if IPYTHON:
IPython.embed()
if not history:
# this is needed to avoid sqlite errors while running in
# a multithreading environment
config.HistoryAccessor.enabled = False

else:
code.interact(local=globals())
context = self.context # NOQA
history = self.history # NOQA

IPython.embed(config=config)

else:
code.interact(local=globals())

finally:
self._shell_running = False

# watcher events ##########################################################
def handle_watcher_events(self, events):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'live-server': [
'aiohttp-json-rpc==0.12.1',
'pygments',
'ipython<7',
],
'chardet': [
'chardet',
Expand All @@ -40,7 +41,7 @@
'coloredlogs',
],
'ipython': [
'ipython',
'ipython<7',
]
}

Expand Down

0 comments on commit 695341d

Please sign in to comment.