Skip to content

Instantly share code, notes, and snippets.

@ionrock
Created February 9, 2016 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionrock/5947c5378aa8b4f09538 to your computer and use it in GitHub Desktop.
Save ionrock/5947c5378aa8b4f09538 to your computer and use it in GitHub Desktop.
import collections
import itertools
from functools import partial
def foo(input):
return 1
def check(v):
return v == 1
def bar(input):
return 'bar'
def baz(input):
return 'baz'
class IfTask(object):
def __init__(self, check, a, b):
self.check = check
self.a = a
self.b = b
def __call__(self, input):
if check(input):
return do_flow(self.a, input)
return do_flow(self.b, input)
def finish(input):
print('done: %s' % input)
return input
def do_flow(flow, result=None):
for item in flow:
print(item, result)
result = item(result)
return result
myflow = [
foo,
IfTask(check, [bar], [baz]),
finish,
]
def main():
print(do_flow(myflow))
if __name__ == '__main__':
main()
@TimSimmons
Copy link

class ResultThread(threading.Thread):

    def __init__(self, func, *args, **kwargs):
        super(ResultThread, self).__init__(*args, **kwargs)

        self.func = func
        self.return_value = None

    def run(self):
        self.return_value = self.func()

    def some func():
        threads = []
        for target in get_targets():
            t = ResultThread(CreateZoneOnTarget(zone=self.zone,
                                                target=target),
                             name='czot-%s-%s' % (self.zone,
                                                  target))
            t.start()
            threads.append(t)

        self.state['results'] = join_results(threads)

def join_results(threads):
    results = []
    for t in threads:
        t.join()
        results.append(t.return_value)

    return results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment