Skip to content

Commit

Permalink
This is a real hack of a change that should go away as soon as possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtiss committed Aug 10, 2010
1 parent 0579068 commit d129bc6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions phdev_ensure_devfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shrapnel
3 changes: 3 additions & 0 deletions shrapnel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from shrapnel.version import *

__version__ = VERSION
41 changes: 41 additions & 0 deletions shrapnel/classtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,47 @@ def __call__(self):
pass


class PooledFunction(object):
def __new__(cls, *args, **kwargs):
instance = super(PooledFunction, cls).__new__(cls)
result = None

if isinstance(instance, cls):
super(cls, )
instance.__init__()
instance.__
result = instance()

if isinstance(result, cls):
raise RuntimeError("{0.__name__}.__call__ cannot return an instance of {0.__name__}, otherwise the __new__ mechanism would call __init__ twice.".format(cls))

kwargs['pool']


@classmethod
def reconstruct_and_execute(cls, args, kwargs):
cls(None, None, None, *args, **kwargs)


def __init__(self, pool, handler, callback, *args, **kwargs):
self.pool = pool
self.handler = handler
self.callback = callback
self.args = args
self.kwargs = kwargs
super(PooledFunction, self).__init__(*args, **kwargs)

def __call__(self):
self.pool.apply_async(self.reconstruct_and_execute, (self.args, self.kwargs), dict(), self._callback)

def _callback(self, result):
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(functools.partial(self.callback, result))

def execute(self):
pass


class BackgroundFunction(UserFunction):
def __init__(self, callback = None):
self.callback = callback
Expand Down
13 changes: 13 additions & 0 deletions shrapnel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ def __defaults__(self):
)


class ProcessPoolProvider(SingletonProvider, Provider):
__abstract__ = True

def construct(self, config):
import multiprocessing
return multiprocessing.Pool(config['processes'])

def __defaults__(self):
return dict(
processes = 10
)


class MongoProvider(Provider):
__abstract__ = True

Expand Down
18 changes: 18 additions & 0 deletions shrapnel/poolit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# encoding: utf-8
"""
poolit.py
Created by Kurtiss Hare on 2010-08-10.
Copyright (c) 2010 Medium Entertainment, Inc. All rights reserved.
"""

import tornado.ioloop
import functools


def poolit(pool, callback, func, args, kwargs):
def wrapper(result):
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(functools.partial(callback, result))
pool.apply_async(func, args, kwargs, wrapper)

0 comments on commit d129bc6

Please sign in to comment.