Package: netsight.async
Introductionnetsight.async provides a base browser view for the Zope Web Framework
which enables browser requests to run in the background whilst progress
of the request is returned to the browser.UsageBasicFirst, subclass theBaseAsyncViewclass. Where you might ordinarily
write code in the__call__method of a view class, to perform some
process, instead, place it in the__run__method.>>> import time
>>> from netsight.async.browser.BaseAsyncView import BaseAsyncView
>>> class MyView(BaseAsyncView):
...
...    def __run__(self, *args, **kwargs):
...         time.sleep(30)
...         return "Hello world!"
...
>>>When you call this view from the browser, it will display whatever
output is configured as normal. When you perform a POST request to the
view, however, the__run__method will be called in the background
as if it were the__call__method of the view class. Meanwhile, a
page displaying a spinner will be returned to the browser and will poll
at 5 second intervals until the process defined in__run__completes.Once the__run__method has completed, the browser will redirect to
the result.Example timeline:User visits ‘/myview’ and is shown some form.User submits the form, the process is started in the background.The user is redirected to
‘/myview/processing?process_id=abcde-f01234’ which shows a spinner.The user’s current page polls for process status for up to 30
seconds via AJAX if possible, otherwise by page refresh.Once the process has completed, the user is redirected to
‘/myview/result?process_id=abcde-f01234’ and shown “Hello world!”Using with page templatesIf you have configured a browser view for your view class with a
page template file specified in ZCML, this will be shown by default
when the view is first called. If the view is POSTed to, then the
process will be kick-started. You can change the initial template and
the conditions under which the process is started by overriding theinitial_pageand therun_processmethods of the view.>>> from Products.Five.browser.pagetemplatefile import \
...      ViewPageTemplateFile
>>>
>>> class MyView(BaseAsyncView):
...
...     def run_process(self):
...         return 'run' in self.request.form
...
...     initial_page = \
...         ViewPageTemplateFile('templates/my_template.pt')
...
>>>Or you could use a method:>>> class MyView(BaseAsyncView):
...
...     def initial_page(self, *args, **kwargs):
...         return 'Hello world!'
...
>>>You can also override the page returned to the browser once the process
has been started by overriding the processing_page method.If you want to call a template defined in ZCML from your__run__method, you may pass aTruevalue namedno_process` to the call method if your ``run_processmethod would ordinarily start the
process again.>>> class MyView(BaseAsyncView):
...
...     def __run__(self, *args, **kwargs):
...         return self.__call__(message="Hello world",
...                              no_process=True)
...
>>>Checking the status & retrieving the resultOnce you have kicked off your__run__method,  the resulting
response will redirect to the processing view, with a unique ID for the
newly started process given as a GET variable,process_id.This process ID can be used to retrieve information on the status of
the process and its result.Calling thecompletedmethod of the view with the process ID will
return either aTrueorFalsecompletion state, or a number
representing a percentage completion out of 100 (more on recording
progress later). If the optional argument,output_jsonis set to
some value which evaluates toTrue, the method returns a JSON
object with the single key,completedcontaining the sameTrue,Falseor numeric value.If your process died before it completed, it will raise an error, or if
JSON output is chosen, it will return acompletedvalue of the
string, ‘ERROR’.To retrieve the result of the__run__method once it has completed,
call theresultmethod of the view with the process ID.If your process died before it completed, this too will raise an error,
or if JSON output is chosen, it will return acompletedvalue of
the string, ‘ERROR’.If the process has not yet completed whenresultis called,Nonewill be returned.Setting process progress from your taskIf you want your task to return some measure of completion you can call
theset_progressmethod with the process ID and some numeric value.>>> class MyView(BaseAsyncView):
...
...    def __run__(self, process_id=None, *args, **kwargs):
...         time.sleep(15)
...         self.set_progress(process_id, 50)
...         time.sleep(15)
...         return "Hello world!"
...
>>>When your task completes without raising an exception, the progress is
automatically set to 100 so there is no need to set this before the
method returns.InstallationSimply addnetsight.asyncto theeggssection of your buildout
configuration. If you also plan on using the stock ‘processing’ page,
you may also need to add it to thezcmlsection.[buildout]
eggs = ...
       netsight.async
zcml = ...
       netsight.asyncLimitationsBecause running the new process cannot be done using existing threads
from the Zope pool, for the duration of the asynchronous process, an
extra thread is created by the Zope process, beyond the normal thread
limit. This also means an extra connection is opened to the ZODB beyond
the normal connection limit which may cause a warning to be shown in
either the console or log files.Once the__run__method has started, it cannot be stopped by the
user in any way. This a feature that subclasses may implement if
they choose, but would be dangerous to implement in this package
without knowledge of what the background task was doing & what cleanup
may be required.To be improvedCurrently processes are stored in memory of a particular Python
instance. This introduces the following issues:If the user never retrieves the results from the__run__method, they are stored in the ZODB permanently.DependenciesPython>=2.4.0zope.component>=3.4.0zope.i18n>=3.4.0zope.i18nmessageid>=3.4.0zope.publisherZope>=2.8.0The default processing page template depends on a main template being,
provided, much like the one provided by Products.CMFPlone, however
this may be overridden by your own view, as discussed above.ContributionsYou can find the source code for this project at:http://github.com/netsight/netsight.asyncThis product needs translations! There are only 2 strings to do, so
this is a really quick and easy way to contribute to an open-source
project.Any bug fixes, new features & documentation improvements are welcome,
just submit a pull request on github.Changelog1.1.1 - (2011-10-27)Fix process completion detection when progress is set to 100.1.1.0 - (2011-10-27)Made Python 2.4 compatible.Process progress/result storage is now held in the ZODB, rather than
in a Python instance’s memory.1.0.1 - (2011-10-19)Fixed package manifest.Fixed markup on processing page.1.0.0 - (2011-10-19)Initial release.LicenseCopyright 2011 Netsight Internet Solutions LimitedLicensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.