fork download
  1. import sys
  2.  
  3. class multi_threaded:
  4. def __init__(self, count):
  5. self.count = count
  6.  
  7. def __enter__(self):
  8. self.old_locals = sys._getframe(1).f_locals.copy()
  9.  
  10. def __exit__(self, exc_type, exc_val, exc_tb):
  11. new_locals = sys._getframe(1).f_locals
  12. funcs = list(map(new_locals.get, new_locals.keys() - self.old_locals.keys()))
  13. my_slice = int(self.count / len(funcs))
  14. for i, func in enumerate(funcs):
  15. start = my_slice * i
  16. func(start, start + my_slice)
  17.  
  18. def spawn_many():
  19. dataset = [1, 2, 3, 4, 5]
  20. with multi_threaded(len(dataset)):
  21. def foo(start_idx, end):
  22. print("foo" + str(dataset[start_idx : end]))
  23. def bar(start_idx, end):
  24. print("bar" + str(dataset[start_idx : end]))
  25.  
  26. spawn_many()
Success #stdin #stdout 0.04s 9752KB
stdin
Standard input is empty
stdout
bar[1, 2]
foo[3, 4]