Example #1
0
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from qcore import MarkerObject
from asynq import asynq, async_proxy, result, ConstFuture
from collections import deque

# TODO(alex): finish w/this test

empty = MarkerObject(u'empty @ asynq.channels')
future_empty = ConstFuture(empty)
future_false = ConstFuture(False)
future_true = ConstFuture(True)


class Channel(object):
    def __init__(self, capacity=256):
        self.capacity = capacity
        self.items = deque()
        self.futures = deque()

    @async_proxy(pure=True)
    def push(self, value, should_await=True):
        if self.futures:
            future = self.futures.popleft()
            future.set_value(value)
Example #2
0
 def pull(self, should_await=True):
     if self.items:
         return ConstFuture(self.items.popleft())
     return _pull_async(self) \
         if should_await else future_empty
Example #3
0
def proxied_fn():
    return ConstFuture("capybaras!")
Example #4
0
def async_proxied_fn():
    return ConstFuture("async_proxied_fn")
Example #5
0
 def get(self, key):
     self._dependency(key)
     value = self._local.get(key)
     if value is miss:  # No item is cached locally
         return self._create_batch_item("get", key)
     return ConstFuture(miss if value is none else value)
Example #6
0
def async_proxied_fn():
    return ConstFuture('async_proxied_fn')
Example #7
0
def test_result():
    with AssertRaises(AssertionError):
        result(ConstFuture(None))
    with AssertRaises(AsyncTaskResult):
        result(None)