Skip to content

Commit

Permalink
backends: allow configuration via dictionary (#10)
Browse files Browse the repository at this point in the history
* backends: allow configuration via dictionary

* * add log
  • Loading branch information
lukasheinrich authored Oct 10, 2017
1 parent e6f52dd commit 31aa524
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packtivity/asyncbackends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import sys
import traceback
import os
import logging

from .syncbackends import run_packtivity
from .syncbackends import prepublish
from .syncbackends import packconfig

log = logging.getLogger(__name__)

class PacktivityProxyBase(object):
'''
A generic serializable proxy wrapper around a proxy object,
Expand Down Expand Up @@ -59,6 +62,8 @@ def __init__(self,poolsize, packconfig_spec = None):
super(MultiProcBackend,self).__init__(packconfig_spec)
if poolsize == 'auto':
poolsize = multiprocessing.cpu_count()

log.info('configured pool size to %s', poolsize)
self.pool = multiprocessing.Pool(int(poolsize))

def submit_callable(self,callable):
Expand Down
30 changes: 19 additions & 11 deletions packtivity/backendutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import yaml
import packtivity.asyncbackends as asyncbackends
import packtivity.syncbackends as syncbackends
import logging

log = logging.getLogger(__name__)


def proxy_from_json(jsondata, best_effort_backend = True, raise_on_unknown = False):
proxy, backend = None, None
Expand Down Expand Up @@ -36,37 +40,41 @@ def proxy_from_json(jsondata, best_effort_backend = True, raise_on_unknown = Fal
return proxy, backend
return proxy

def backend_from_string(backendstring):
def backend_from_string(backendstring,backendopts = None):
'''
creates (a)sync backends from strings
returns tuple (boolean,backend) where boolean
specifies whether this is a syncbackend (True) or
asyncbackend (False)
'''
backendopts = backendopts or {}
ctor_kwargs = os.environ.get('PACKTIVITY_ASYNCBACKEND_OPTS',{})
if ctor_kwargs:
ctor_kwargs = yaml.load(open(ctor_kwargs))
log.info('overriding using envvar opts %s', ctor_kwargs)
backendopts.update(**ctor_kwargs)
is_sync, is_async = True, False
if backendstring == 'defaultsync':
return is_sync, syncbackends.defaultsyncbackend()
return is_sync, syncbackends.defaultsyncbackend(**backendopts)
if backendstring.startswith('multiproc'):
_,poolsize = backendstring.split(':')
backend = asyncbackends.MultiProcBackend(poolsize = poolsize)
backendopts.update(poolsize = poolsize)
backend = asyncbackends.MultiProcBackend(**backendopts)
return is_async, backend

if backendstring == 'foregroundasync':
backend = asyncbackends.ForegroundBackend()
backend = asyncbackends.ForegroundBackend(**backendopts)
return is_async, backend

if backendstring == 'ipcluster':
backend = asyncbackends.IPythonParallelBackend()
backend = asyncbackends.IPythonParallelBackend(**backendopts)
return is_async, backend
if backendstring == 'celery':
backend = asyncbackends.CeleryBackend()
backend = asyncbackends.CeleryBackend(**backendopts)
return is_async, backend
if backendstring == 'fromenv':
module, backend, _ = os.environ['PACKTIVITY_ASYNCBACKEND'].split(':')
ctor_kwargs = os.environ.get('PACKTIVITY_ASYNCBACKEND_OPTS',{})
if ctor_kwargs:
ctor_kwargs = yaml.load(open(ctor_kwargs))
module = importlib.import_module(module)
backendclass = getattr(module,backend)
return is_async, backendclass(**ctor_kwargs)
raise RuntimeError('Unknown Backend')
return is_async, backendclass(**backendopts)
raise RuntimeError('Unknown Backend %s', backendstring)
3 changes: 2 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[pytest]
addopts = --ignore=setup.py --cov=packtivity --cov-report=term-missing --cov-config=.coveragerc
addopts = --ignore=setup.py --cov=packtivity --cov-report=term-missing --cov-config=.coveragerc --cov-report html

0 comments on commit 31aa524

Please sign in to comment.