Skip to content

Commit

Permalink
reentrancy and smal bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdremov committed Mar 22, 2023
1 parent dcc9582 commit e97baac
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 25 deletions.
1 change: 1 addition & 0 deletions bld.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$PYTHON setup.py install
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"%PYTHON%" setup.py install
if errorlevel 1 exit 1
16 changes: 11 additions & 5 deletions igogo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import IPython
from .yielder import Yielder as yielder
from .core import job, stop
from .yielder import Yielder as yielder_async
from .core import job, stop, run
from .loaders import register_hooks as _register_hooks
from .loaders import IpythonWatcher
from .loaders import load_ipython_extension

_ip = IPython.get_ipython()
_watcher = IpythonWatcher(_ip)
_register_hooks(_watcher, _ip)
yielder = yielder_async.igogo_await

__igogo_inited = False

if not __igogo_inited:
__igogo_inited = True
_ip = IPython.get_ipython()
_watcher = IpythonWatcher(_ip)
_register_hooks(_watcher, _ip)
1 change: 1 addition & 0 deletions igogo/awaiters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import asyncio
40 changes: 36 additions & 4 deletions igogo/core.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
import asyncio
import functools
import greenback

from .context import IgogoContext, get_context_or_fail, set_context
from .output import Output, OutputStream
from .exceptions import IgogoInvalidContext

_igogo_run_loop = asyncio.get_running_loop()
_igogo_count = 0


def stop():
value = get_context_or_fail()
value.task.cancel()


def sleep(delay, result=None):
if not greenback.has_portal():
raise IgogoInvalidContext()
greenback.await_(asyncio.sleep(delay, result))


def run():
def get_pending():
return list(filter(lambda x: 'igogo' in x.get_name(), asyncio.all_tasks(loop=_igogo_run_loop)))

pending_all = get_pending()
while len(pending_all):
pending = pending_all[-1]
_igogo_run_loop.run_until_complete(pending)
pending_all = get_pending()


def job(original_function=None, kind='text'):
global _igogo_count

def _decorate(function):
@functools.wraps(function)
def wrapped_function(*args, **kwargs):
global _igogo_count
output_stream = OutputStream(Output(kind=kind))
output_stream.activate()

async def func_context_setter():
await greenback.ensure_portal()
set_context(
IgogoContext(task, output_stream)
)
await function(*args, **kwargs)
output_stream.activate()
return await function(*args, **kwargs)

coro = func_context_setter()

Expand All @@ -42,16 +67,23 @@ def done_callback(t):
pass

task.add_done_callback(done_callback)
task.set_name(f'igogo_{_igogo_count}')
_igogo_count += 1

return task
return dict(
task=task,
output=output_stream.out
)

def stopall():
def stop_all():
if not hasattr(wrapped_function, "tasks"):
wrapped_function.tasks = []
for task in wrapped_function.tasks:
task.cancel()

from .yielder import Yielder
wrapped_function.yielder = Yielder
wrapped_function.stopall = stopall
wrapped_function.stop_all = stop_all
wrapped_function.stop = stop
return wrapped_function

Expand Down
7 changes: 3 additions & 4 deletions igogo/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ def __init__(self, ip: ZMQInteractiveShell):
self.shell = ip

def pre_execute(self):
...
stream = OutputStream(Output())
stream.activate()

def pre_run_cell(self, info):
out = Output()
stream = OutputStream(out)
stream.activate()
...

def post_execute(self):
...
Expand Down
13 changes: 9 additions & 4 deletions igogo/magic.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import IPython
from IPython.core.magic import (Magics, magics_class, cell_magic)
from .output import Output

@magics_class
class IgogoMagic(Magics):
@cell_magic
def igogo(self, line, cell):
ip = IPython.get_ipython()
prefix = "def __igogo_magic_wrapper():\n import igogo\n @igogo.job\n async def execute():\n"
prefix = "def __igogo_magic_wrapper():\n" \
" import igogo\n" \
f" @igogo.job(**dict({line}))\n" \
" async def execute():\n"
cell = prefix + '\n'.join([' ' + line for line in cell.split('\n')])
cell += " execute()\n__igogo_magic_wrapper()"
ip.run_cell(cell)
return None
cell += "\n" \
" return execute()\n" \
"__igogo_magic_wrapper()"
ip.ex(cell)
15 changes: 14 additions & 1 deletion igogo/yielder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from .core import get_context_or_fail
from .exceptions import IgogoInvalidContext

import greenback


class Yielder:

@classmethod
def igogo_await(cls):
if not greenback.has_portal():
raise IgogoInvalidContext()
greenback.await_(cls())

def __await__(self):
value = get_context_or_fail()
try:
value = get_context_or_fail()
except IgogoInvalidContext:
return
yield
value.out_stream.activate()
21 changes: 17 additions & 4 deletions meta.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set version = "1.1.0" %}
{% set version = "0.0.5" %}


package:
Expand All @@ -10,12 +10,25 @@ source:
git_url: https://github.com/AlexRoar/igogo.git

requirements:
build:
host:
- python
- setuptools

run:
build:
- python
run:
- IPython
- ipykernel
- greenback

about:
home: https://github.com/AlexRoar/igogo
license: MIT
license_familY: MIT
license_file: LICENSE
summary: 'Execute several jupyter cells simultaneously'
description: |
IPython framework that allows you to run several cells at the same time
with correct and gorgeous output
dev_url: https://github.com/AlexRoar/igogo
doc_url: https://github.com/AlexRoar/igogo
doc_source_url: hhttps://github.com/AlexRoar/igogo/blob/main/README.me
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "igogo"
version = "0.0.4"
version = "0.0.5"
authors = [
{ name = "Alex Dremov", email = "igogo@alexdremov.me" },
]
Expand All @@ -19,8 +19,9 @@ classifiers = [
]
keywords = ["jupyterlab", "ipython", "jupyter", "execute", "python"]
dependencies = [
'IPython',
'ipykernel'
'IPython >= 8.0.0',
'ipykernel >= 6.0.0',
'greenback >= 1.1.1'
]
packages = [
{ include = "igogo" }
Expand Down

0 comments on commit e97baac

Please sign in to comment.