text
stringlengths
0
828
else:
raise RuntimeError('invalid job state {0!r}'.format(self.__state))"
439,"def finished(self):
""""""
True if the job run and finished. There is no difference if the job
finished successfully or errored.
""""""
return self.__state in (Job.ERROR, Job.SUCCESS, Job.CANCELLED)"
440,"def get(self, default=None):
""""""
Get the result of the Job, or return *default* if the job is not finished
or errored. This function will never explicitly raise an exception. Note
that the *default* value is also returned if the job was cancelled.
# Arguments
default (any): The value to return when the result can not be obtained.
""""""
if not self.__cancelled and self.__state == Job.SUCCESS:
return self.__result
else:
return default"
441,"def cancel(self):
""""""
Cancels the job. Functions should check the #Job.cancelled flag from time
to time to be able to abort pre-emptively if the job was cancelled instead
of running forever.
""""""
with synchronized(self):
cancelled = self.__cancelled
if not cancelled:
self.__cancelled = True
notify_all(self)
if not cancelled:
self._trigger_event(Job.CANCELLED)"
442,"def _trigger_event(self, event):
""""""
Private. Triggers and event and removes all one-off listeners for that event.
""""""
if event is None or event not in self.__listeners:
raise ValueError('invalid event type: {0!r}'.format(event))
# Check the event has not already been triggered, then mark
# the event as triggered.
if event in self.__event_set:
raise RuntimeError('event already triggered: {0!r}'.format(event))
self.__event_set.add(event)
listeners = self.__listeners[event] + self.__listeners[None]
# Remove one-off listeners.
self.__listeners[event][:] = (l for l in self.__listeners[event] if not l.once)
self.__listeners[None][:] = (l for l in self.__listeners[None] if not l.once)
for listener in listeners:
# XXX: What to do on exceptions? Catch and make sure all listeners
# run through? What to do with the exception(s) then?
listener.callback(self, event)"
443,"def add_listener(self, event, callback, once=False):
""""""
Register a *callback* for the specified *event*. The function will be
called with the #Job as its first argument. If *once* is #True, the
listener will be removed after it has been invoked once or when the
job is re-started.
Note that if the event already ocurred, *callback* will be called
immediately!
# Arguments
event (str, list of str): The name or multiple names of an event, or None
to register the callback to be called for any event.
callback (callable): A function.
once (bool): Whether the callback is valid only once.
""""""
if not callable(callback):
raise TypeError('callback must be callable')
if isinstance(event, str):
event = [event]
for evn in event:
if evn not in self.__listeners:
raise ValueError('invalid event type: {0!r}'.format(evn))
for evn in event:
event_passed = False
with synchronized(self):
event_passed = (evn in self.__event_set)
if not (once and event_passed):
self.__listeners[evn].append(Job._Listener(callback, once))
# If the event already happened, we'll invoke the callback
# immediately to make up for what it missed.
if event_passed:
callback(self, event)"
444,"def wait(self, timeout=None):
""""""
Waits for the job to finish and returns the result.