Skip to content

Commit

Permalink
Use a UUID (rather than a sentinel object) on Pub / Sub Future. (#4634
Browse files Browse the repository at this point in the history
)

Also changing identity check to equality check for Future._SENTINEL.
  • Loading branch information
dhermes authored Dec 20, 2017
1 parent 190e148 commit 1bbc457
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pubsub/google/cloud/pubsub_v1/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import absolute_import

import threading
import uuid

import google.api_core.future
from google.cloud.pubsub_v1.publisher import exceptions
Expand All @@ -29,7 +30,11 @@ class Future(google.api_core.future.Future):
This object should not be created directly, but is returned by other
methods in this library.
"""
_SENTINEL = object()

# This could be a sentinel object or None, but the sentinel object's ID
# can change if the process is forked, and None has the possibility of
# actually being a result.
_SENTINEL = uuid.uuid4()

def __init__(self):
self._result = self._SENTINEL
Expand Down Expand Up @@ -68,8 +73,8 @@ def done(self):
This still returns True in failure cases; checking :meth:`result` or
:meth:`exception` is the canonical way to assess success or failure.
"""
return (self._exception is not self._SENTINEL or
self._result is not self._SENTINEL)
return (self._exception != self._SENTINEL or
self._result != self._SENTINEL)

def result(self, timeout=None):
"""Return the message ID, or raise an exception.
Expand Down Expand Up @@ -118,7 +123,7 @@ def exception(self, timeout=None):
raise exceptions.TimeoutError('Timed out waiting for result.')

# If the batch completed successfully, this should return None.
if self._result is not self._SENTINEL:
if self._result != self._SENTINEL:
return None

# Okay, this batch had an error; this should return it.
Expand Down

0 comments on commit 1bbc457

Please sign in to comment.