def __init__(self, n):
self.n = n
self.count = 0
self.mutex = Semaphore(1)
self.barrier = Semaphore(0)
python类Semaphore()的实例源码
def test_timer_is_well_created_and_delayed(self):
sem = threading.Semaphore(1)
def delayed():
sem.release()
lock_time = time.time()
sem.acquire()
caduc.timer.Timer(1, delayed).start()
sem.acquire()
release_time = time.time()
(release_time-lock_time).should.be.eql(1., epsilon=0.1)
def setUp(self):
self.faker = faker.Faker()
self.semaphore = mock.Mock()
self.original_semaphore = threading.Semaphore
threading.Semaphore = mock.MagicMock(return_value=self.semaphore)
self.semaphore.reset_mock()
def tearDown(self):
threading.Semaphore = self.original_semaphore
def test_init_default_count(self):
ClientSemaphore()
threading.Semaphore.assert_called_once_with(5)
def test_init_provided_count(self):
threading.Semaphore = mock.MagicMock()
ClientSemaphore(10)
threading.Semaphore.assert_called_once_with(10)
def wait_for_property(self, name, cond=lambda val: val, level_sensitive=True):
sema = threading.Semaphore(value=0)
def observer(val):
if cond(val):
sema.release()
self.observe_property(name, observer)
if not level_sensitive or not cond(getattr(self, name.replace('-', '_'))):
sema.acquire()
self.unobserve_property(name, observer)
def __init__(self):
self.mutex = threading.RLock()
self.can_read = threading.Semaphore(0)
self.can_write = threading.Semaphore(0)
self.active_readers = 0
self.active_writers = 0
self.waiting_readers = 0
self.waiting_writers = 0
def __init__(self):
self._profile = None
self._lstVdfProfiles = self.builder.get_object("tvVdfProfiles").get_model()
self._q_games = collections.deque()
self._q_profiles = collections.deque()
self._s_games = threading.Semaphore(0)
self._s_profiles = threading.Semaphore(0)
self._lock = threading.Lock()
self.__profile_load_started = False
self._on_preload_finished = None
def __init__(self,
training_label_prefix,
dataset_name=None,
epochs=None,
time_limit=None,
num_gpus=None):
if not ((epochs is None) ^ (time_limit is None)):
raise ValueError('epochs or time_limit must present, '
'but not both!')
self._training_label_prefix = training_label_prefix
self._dataset_name = dataset_name or active_config().dataset_name
self._validate_training_label_prefix()
self._epochs = epochs
self._time_limit = time_limit
fixed_config_keys = dict(dataset_name=self._dataset_name,
epochs=self._epochs,
time_limit=self._time_limit)
self._config_builder = Embed300FineRandomConfigBuilder(
fixed_config_keys)
try:
self._num_gpus = len(sh.nvidia_smi('-L').split('\n')) - 1
except sh.CommandNotFound:
self._num_gpus = 1
self._num_gpus = num_gpus or self._num_gpus
# TODO ! Replace set with a thread-safe set
self._available_gpus = set(range(self.num_gpus))
self._semaphore = Semaphore(self.num_gpus)
self._running_commands = [] # a list of (index, sh.RunningCommand)
self._stop_search = False
self._lock = Lock()
def main(training_label_prefix,
dataset_name=None,
epochs=None,
time_limit=None,
num_gpus=None):
epochs = int(epochs) if epochs else None
time_limit = parse_timedelta(time_limit) if time_limit else None
num_gpus = int(num_gpus) if num_gpus else None
search = HyperparamSearch(training_label_prefix=training_label_prefix,
dataset_name=dataset_name,
epochs=epochs,
time_limit=time_limit,
num_gpus=num_gpus)
def handler(signum, frame):
logging('Stopping hyperparam search..')
with search.lock:
search.stop()
for index, running_command in search.running_commands:
try:
label = search.training_label(index)
logging('Sending SIGINT to {}..'.format(label))
running_command.signal(signal.SIGINT)
except OSError: # The process might have exited before
logging('{} might have terminated before.'.format(label))
except:
traceback.print_exc(file=sys.stderr)
logging('All training processes have been sent SIGINT.')
signal.signal(signal.SIGINT, handler)
# We need to execute search.run() in another thread in order for Semaphore
# inside it doesn't block the signal handler. Otherwise, the signal handler
# will be executed after any training process finishes the whole epoch.
executor = ThreadPoolExecutor(max_workers=1)
executor.submit(search.run)
# wait must be True in order for the mock works,
# see the unit test for more details
executor.shutdown(wait=True)
def __init__(self):
self.tasks_by_path = {}
self.queued_tasks = []
self.semaphore = threading.Semaphore(0)
self._lock = threading.Lock()
def __init__(self, timeFrame=1.0, callLimit=6):
"""
timeFrame = float time in secs [default = 1.0]
callLimit = int max amount of calls per 'timeFrame' [default = 6]
"""
self.timeFrame = timeFrame
self.semaphore = Semaphore(callLimit)
def __init__(self, timeFrame=1.0, callLimit=6):
"""
timeFrame = float time in secs [default = 1.0]
callLimit = int max amount of calls per 'timeFrame' [default = 6]
"""
self.timeFrame = timeFrame
self.semaphore = Semaphore(callLimit)
def __init__(self, timeFrame=1.0, callLimit=6):
"""
timeFrame = float time in secs [default = 1.0]
callLimit = int max amount of calls per 'timeFrame' [default = 6]
"""
self.timeFrame = timeFrame
self.semaphore = Semaphore(callLimit)
def __init__(self, count):
"""A semaphore for the purpose of limiting the number of tasks
:param count: The size of semaphore
"""
self._semaphore = threading.Semaphore(count)
def __init__(self, model_base_dir, num_parallel_predictions=2):
possible_dirs = os.listdir(model_base_dir)
model_dir = os.path.join(model_base_dir, max(possible_dirs))
print("Loading {}".format(model_dir))
self.sess = tf.get_default_session()
loaded_model = tf.saved_model.loader.load(self.sess, ['serve'], model_dir)
assert 'serving_default' in list(loaded_model.signature_def)
input_dict, output_dict = _signature_def_to_tensors(loaded_model.signature_def['serving_default'])
self._input_tensor = input_dict['images']
self._output_dict = output_dict
self.sema = Semaphore(num_parallel_predictions)
def __init__(self, count):
"""A semaphore for the purpose of limiting the number of tasks
:param count: The size of semaphore
"""
self._semaphore = threading.Semaphore(count)
def testMultiPythonThread(self):
import time, threading
class Global:
count = 0
started = threading.Event()
finished = threading.Semaphore(0)
def sleep(self, ms):
time.sleep(ms / 1000.0)
self.count += 1
g = Global()
def run():
with JSContext(g) as ctxt:
ctxt.eval("""
started.wait();
for (i=0; i<10; i++)
{
sleep(100);
}
finished.release();
""")
threading.Thread(target=run).start()
now = time.time()
self.assertEqual(0, g.count)
g.started.set()
g.finished.acquire()
self.assertEqual(10, g.count)
self.assertTrue((time.time() - now) >= 1)
def wait_for_property(self, name, cond=lambda val: val, level_sensitive=True):
sema = threading.Semaphore(value=0)
def observer(val):
if cond(val):
sema.release()
self.observe_property(name, observer)
if not level_sensitive or not cond(getattr(self, name.replace('-', '_'))):
sema.acquire()
self.unobserve_property(name, observer)