I am running http server which serves requests constantly. I would like to have separate "helper" thread, checking if directory in filesystem is available. If the resource is gone, then I would like this helper thread to return value to main thread so I can stop the http server.
def main():
os.chdir(root)
server = HTTPServer(('0.0.0.0', 80), MyHandler)
server.serve_forever()
I was thinking about sth like
def foo(bar, baz):
print 'hello {0}'.format(bar)
return 'foo' + baz
from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=1)
async_result = pool.apply_async(foo, ('world', 'foo')) # tuple of args for foo
# do some other stuff in the main process
return_val = async_result.get() # get the return value from your function.
and stop the server if return_val == "stop"
Is it the best way of doing it?
if you're willing to drop the async stuff (because honestly i don't see your justification for needing it) you can just do
while foo():
server.handle_request()
instead of server.serve_forever()
if you really want to do the check asynchronously you need to show a bit more code of how you intend to use it, do you want it to check constantly? once in a while? triggered by some event?
edit: you can still use a while loop just with the if inside
while True:
if foo():
server.handle_request()
else:
sleep(1) # or if foo is blocking (like when accessing the file system) you can just not sleep