Reputation: 874
Say that you have a singleton to play with (which means that the only way to initialize that to original state is to restart the whole script) and you want to do a specific tasks multiple times on this and get the returned objects. Are there any ways I can do this without disk I/O? I know I can do that with subprocess.check_output()
like How to spawn another process and capture output in python? and file I/O or piping stdio
, but are there any cleaner solutions as simple as same-process communications (Edit: I mean, result = foo()
)?
#the singleton
def foo():
crash_when_got_run_twice()
result = something_fancy()
return result
#the runner
def bar(times):
for i in range(times):
result = magic() # run foo()
aggregate_result(result)
return aggregated_result()
What do you think you can do in magic()
?
Upvotes: 0
Views: 135
Reputation: 77337
On unix-like systems you can fork a subprocess and have it run the singleton. Assuming you've already imported everything needed by the singleton and the singleton itself doesn't touch the disk, it could work. Fair warning: whatever reason this thing was a singleton in the first place may nip you. You can let the multiprocessing package do the heavy lifting for you.
On windows, a new python interpreter is executed and there may be significant state passed between parent and child, which could have harmful effects. But again, it may work...
import multiprocessing as mp
#the singleton
def foo():
crash_when_got_run_twice()
result = something_fancy()
return result
def _run_foo(i):
return foo()
#the runner
def bar(times):
with mp.Pool(min(mp.cpu_count(), times) as pool:
return pool.map(_run_foo, range(times))
Upvotes: 1