Files
deb-python-taskflow/taskflow/engines/worker_based/cache.py
Joshua Harlow 34b358a137 Use standard threading locks in the cache types
Instead of using a reader/writer lock in the cache types
just take advantage of the fact that single, non-mutating
operations on dictionaries are thread-safe in python.

This means we can remove the need to have simple read-only
operations using read-locks and can just use a simpler lock
around write-operations or around read/write operations that
span multiple dictionary operations (like iteration).

Change-Id: I4679275d7fe25fac12a6b05e8aa38da95649f4f6
2014-10-25 22:02:53 -07:00

49 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# 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.
import random
import six
from taskflow.engines.worker_based import protocol as pr
from taskflow.types import cache as base
class RequestsCache(base.ExpiringCache):
"""Represents a thread-safe requests cache."""
def get_waiting_requests(self, tasks):
"""Get list of waiting requests by tasks."""
waiting_requests = []
with self._lock:
for request in six.itervalues(self._data):
if request.state == pr.WAITING and request.task_cls in tasks:
waiting_requests.append(request)
return waiting_requests
class WorkersCache(base.ExpiringCache):
"""Represents a thread-safe workers cache."""
def get_topic_by_task(self, task):
"""Get topic for a given task."""
available_topics = []
with self._lock:
for topic, tasks in six.iteritems(self._data):
if task in tasks:
available_topics.append(topic)
return random.choice(available_topics) if available_topics else None