I’m having trouble developing a standard for returning data from user functions passed and executed in a class which runs the function as a separate thread.
The goal would be to pass the return arguments from the function when requested for by the user.
But what if the function is not finished running when the user requests data from my class? Or if the function fails and hence has no data (but its failure is not critical to the program, meaning raising an exception is excessive)
I had thought to just return None but what if the user function returns None and that is meaningful (ie the function completed successfully)? My next idea was to return the function output in a list and None if no data was ready or available. This would mean the user would have to check if the data was returned or not (check if None or a list) which I find slightly inelegant.
I am curious if there is already a standard for things like this or if anyone has a suggestion that is cleaner.
example:
user call
def foo():
#...do lots of stuff that takes long time
# return whether stuff worked or not (None if worked False if not)
return success
takes_forever = detach(foo)
# ... do other things
# get data from detached function which has hopefully completed
are_you_done = takes_forever.give_me_data()
My class:
class detach:
def __init__(self, func):
self.detached_func = execute_as_thread(func)
def give_me_data(self)
if self.detached_func.is_done(): # function completed and didnt die
return [self.detached_func.output]
else: # something went wrong or function is still running
return None
Best would be to raise exception, if for some reason you want to avoid that return a sepecial object e.g. define a class NoResultsType and return object of such type or may be just the class
and then you can define multiple such types e.g. NoResults, FunctionFailed etc and user can just check them e.g.
or you can return a Result object which can keep original result, states etc