diff --git a/README.rst b/README.rst index 14d0bb0..058830a 100644 --- a/README.rst +++ b/README.rst @@ -22,7 +22,7 @@ Demo of button click handler: .. code-block:: python - @engine.async + @engine.asynchronous def on_button_click(self, *args): self.status_label.setText("Downloading image...") # Run single task in separate thread diff --git a/async_gui/engine.py b/async_gui/engine.py index 5778b13..15953b2 100644 --- a/async_gui/engine.py +++ b/async_gui/engine.py @@ -55,7 +55,7 @@ def __init__(self, pool_timeout=POOL_TIMEOUT): #: main application instance self.main_app = None - def async(self, func): + def asynchronous(self, func): """ Decorator for asynchronous generators. Any :class:`Task`, :class:`ProcessTask` or :class:`GTask` yielded from diff --git a/async_gui/toolkits/kivy.py b/async_gui/toolkits/kivy.py index 9a96f37..a52de9e 100644 --- a/async_gui/toolkits/kivy.py +++ b/async_gui/toolkits/kivy.py @@ -13,3 +13,6 @@ def update_gui(self): EventLoop.window._mainloop() else: EventLoop.idle() + + +engine = KivyEngine() diff --git a/async_gui/toolkits/pygtk.py b/async_gui/toolkits/pygtk.py index c5fd958..8a31c7a 100644 --- a/async_gui/toolkits/pygtk.py +++ b/async_gui/toolkits/pygtk.py @@ -10,3 +10,6 @@ class GtkEngine(Engine): def update_gui(self): if gtk.events_pending(): gtk.main_iteration() + + +engine = GtkEngine() diff --git a/async_gui/toolkits/pyqt.py b/async_gui/toolkits/pyqt.py index a843356..d6d6955 100644 --- a/async_gui/toolkits/pyqt.py +++ b/async_gui/toolkits/pyqt.py @@ -8,4 +8,7 @@ class PyQtEngine(QtEngine): """ PyQt4 support """ - QtCore = QtCore \ No newline at end of file + QtCore = QtCore + + +engine = PyQtEngine() diff --git a/async_gui/toolkits/tk.py b/async_gui/toolkits/tk.py index cfa894c..c697f49 100644 --- a/async_gui/toolkits/tk.py +++ b/async_gui/toolkits/tk.py @@ -8,3 +8,6 @@ class TkEngine(Engine): """ def update_gui(self): self.main_app.update() + + +engine = TkEngine() diff --git a/async_gui/toolkits/wx.py b/async_gui/toolkits/wx.py index 2f94d35..a2e043f 100644 --- a/async_gui/toolkits/wx.py +++ b/async_gui/toolkits/wx.py @@ -8,3 +8,6 @@ class WxEngine(Engine): """ def update_gui(self): self.main_app.Yield() + + +engine = WxEngine() diff --git a/docs/index.rst b/docs/index.rst index 389089e..502a77d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -49,13 +49,13 @@ First create :class:`Engine` instance corresponding to your GUI toolkit (see engine = PyQtEngine() -It contains decorator :meth:`@engine.async ` which allows you to +It contains decorator :meth:`@engine.asynchronous ` which allows you to write asynchronous code in serial way without callbacks. Example button click handler: .. code-block:: python - @engine.async + @engine.asynchronous def on_button_click(self, *args): self.status_label.setText("Downloading image...") # Run single task in separate thread diff --git a/examples/gtk_app.py b/examples/gtk_app.py index a9d8c26..621eaf3 100644 --- a/examples/gtk_app.py +++ b/examples/gtk_app.py @@ -9,11 +9,9 @@ import gtk from async_gui import MultiProcessTask, Task -from async_gui.toolkits.pygtk import GtkEngine +from async_gui.toolkits.pygtk import engine from examples.cpu_work import is_prime, PRIMES -engine = GtkEngine() - class GtkExample: def __init__(self): @@ -32,7 +30,7 @@ def __init__(self): box.show() self.window.show() - @engine.async + @engine.asynchronous def check_primes(self, widget, data=None): t = time.time() self.status.set_label("Checking primes...") diff --git a/examples/kivy_app.py b/examples/kivy_app.py index a28ca54..0c3617b 100644 --- a/examples/kivy_app.py +++ b/examples/kivy_app.py @@ -7,13 +7,11 @@ import time from async_gui.engine import Task, MultiProcessTask -from async_gui.toolkits.kivy import KivyEngine +from async_gui.toolkits.kivy import engine from cpu_work import is_prime, PRIMES -engine = KivyEngine() - class MyApp(App): def build(self): grid = GridLayout(cols=4) @@ -24,7 +22,7 @@ def build(self): grid.add_widget(self.status) return grid - @engine.async + @engine.asynchronous def cpu_bound(self, *_): t = time.time() self.status.text = "calculating..." diff --git a/examples/qt_app.py b/examples/qt_app.py index 45edb77..817324f 100644 --- a/examples/qt_app.py +++ b/examples/qt_app.py @@ -8,7 +8,7 @@ from PyQt4 import QtGui from async_gui.engine import Task -from async_gui.toolkits.pyqt import PyQtEngine +from async_gui.toolkits.pyqt import engine if sys.version_info[0] == 3: from urllib.request import urlopen @@ -16,9 +16,6 @@ from urllib import urlopen -engine = PyQtEngine() - - class MainWidget(QtGui.QWidget): def __init__(self, parent=None): @@ -35,7 +32,7 @@ def __init__(self, parent=None): layout.addWidget(self.result_label) layout.addWidget(self.image_label) - @engine.async + @engine.asynchronous def on_button_click(self, *args): self.status_label.setText("Downloading image...") # Run single task in separate thread @@ -56,7 +53,7 @@ def on_button_click(self, *args): def load_url(self, url): return urlopen(url).read() - @engine.async + @engine.asynchronous def check_primes(self, checked): t = time.time() self.status_label.setText("Checking primes...") diff --git a/examples/qt_app_unordered_gen.py b/examples/qt_app_unordered_gen.py index d13f253..10af10f 100644 --- a/examples/qt_app_unordered_gen.py +++ b/examples/qt_app_unordered_gen.py @@ -15,7 +15,7 @@ from async_gui import MultiTask from async_gui.engine import Task -from async_gui.toolkits.pyqt import PyQtEngine +from async_gui.toolkits.pyqt import engine if sys.version_info[0] == 3: @@ -24,9 +24,6 @@ from urllib import urlopen -engine = PyQtEngine() - - class MainWidget(QtGui.QWidget): urls = [ @@ -74,7 +71,7 @@ def __init__(self, parent=None): layout.addLayout(hbox) layout.addWidget(self.urls_table) - @engine.async + @engine.asynchronous def on_button_click(self, *args): t = time.time() self.status_label.setText("Downloading pages...") diff --git a/examples/tk_app.py b/examples/tk_app.py index c9bff44..8bae682 100644 --- a/examples/tk_app.py +++ b/examples/tk_app.py @@ -7,14 +7,11 @@ import time from async_gui.engine import Task, MultiProcessTask -from async_gui.toolkits.tk import TkEngine +from async_gui.toolkits.tk import engine from cpu_work import is_prime, PRIMES -engine = TkEngine() - - class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master, width=800, height=400) @@ -36,7 +33,7 @@ def __init__(self, master=None): self.status.append(tk.Label(self)) self.status[i].grid(column=1, row=1 + i, sticky=tk.W) - @engine.async + @engine.asynchronous def cpu_bound(self): t = time.time() self.status[0]["text"] = "calculating..." diff --git a/examples/wx_app.py b/examples/wx_app.py index 7f3e9b0..22c9c52 100644 --- a/examples/wx_app.py +++ b/examples/wx_app.py @@ -5,16 +5,12 @@ import time import wx -from async_gui.toolkits.wx import WxEngine +from async_gui.toolkits.wx import engine from async_gui.engine import Task, MultiProcessTask from cpu_work import is_prime, PRIMES -engine = WxEngine() -async = engine.async - - class Example(wx.Frame): def __init__(self, parent, title): @@ -43,7 +39,7 @@ def InitUI(self): sizer.AddGrowableRow(2) panel.SetSizerAndFit(sizer) - @engine.async + @engine.asynchronous def cpu_bound(self, event): t = time.time() self.status.SetLabel("calculating...") diff --git a/tests/test_engine.py b/tests/test_engine.py index fc469db..f75ab17 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -14,7 +14,7 @@ engine = Engine() -async = engine.async +asynchronous = engine.asynchronous class EngineTestCase(unittest.TestCase): @@ -35,7 +35,7 @@ def test_async(self): def test_async_with_return_before_yield(self): # issue 1 called = [False] - @async + @asynchronous def func(): called[0] = True return @@ -49,21 +49,21 @@ def shouldnt_call_this(): self.assertEquals(called[0], True) def test_async_with_result(self): - @async + @asynchronous def func(): r = yield self.Task(self.simple_method) return_result(r) self.assertEquals(func(), 42) def test_async_process(self): - @async + @asynchronous def func(): r = yield self.ProcessTask(mp_func, self._main_process) return_result(r) self.assertEquals(func(), 42) def test_async_multiprocess(self): - @async + @asynchronous def func(): n = 10 tasks = [self.ProcessTask(mp_func, self._main_process) @@ -82,7 +82,7 @@ def func(): def test_multitask_unordered(self): n = 100 - @async + @asynchronous def async_exec(tasks, skip_errors): gen = yield self.MultiTask(tasks, unordered=True, skip_errors=skip_errors) @@ -97,7 +97,7 @@ def async_exec(tasks, skip_errors): tasks.append(self.Task(self.throwing, "test")) self.assertEquals(len(async_exec(tasks, skip_errors=True)), n + 1) - @async + @asynchronous def async_method(self): def check_the_same_thread(): return thread.get_ident() == self._main_thread diff --git a/tests/test_gevent.py b/tests/test_gevent.py index eb3fad3..c874788 100644 --- a/tests/test_gevent.py +++ b/tests/test_gevent.py @@ -13,7 +13,7 @@ else: patch_socket() from async_gui.gevent_tasks import MultiGTask - from tests.test_engine import EngineTestCase, engine, async + from tests.test_engine import EngineTestCase, engine, asynchronous class GeventExecutorTestCase(EngineTestCase): @@ -25,7 +25,7 @@ class GeventExecutorTestCase(EngineTestCase): def test_gevent_urllib(self): self.gevent_with_urllib() - @async + @asynchronous def gevent_with_urllib(self): def download(url): diff --git a/tests/test_toolkits.py b/tests/test_toolkits.py index 52521f4..cf07fc6 100644 --- a/tests/test_toolkits.py +++ b/tests/test_toolkits.py @@ -107,7 +107,7 @@ def task_func(arg): time.sleep(0.2) # timeout to let update_gui() be called return arg - @engine.async + @engine.asynchronous def async_gen(): answer = yield [Task(task_func, i) for i in range(N)] self.assertEquals(answer, list(range(N)))