本文整理汇总了Python中threading.Semaphore方法的典型用法代码示例。如果您正苦于以下问题:Python threading.Semaphore方法的具体用法?Python threading.Semaphore怎么用?Python threading.Semaphore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading
的用法示例。
在下文中一共展示了threading.Semaphore方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(
self, config: CosmosDbConfig, client: cosmos_client.CosmosClient = None
):
"""Create the storage object.
:param config:
"""
super(CosmosDbStorage, self).__init__()
self.config = config
self.client = client or cosmos_client.CosmosClient(
self.config.endpoint, {"masterKey": self.config.masterkey}
)
# these are set by the functions that check
# the presence of the database and container or creates them
self.database = None
self.container = None
self._database_creation_options = config.database_creation_options
self._container_creation_options = config.container_creation_options
self.__semaphore = Semaphore()
示例2: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self, max_workers=None, thread_name_prefix=''):
"""Initializes a new ThreadPoolExecutor instance.
Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
thread_name_prefix: An optional name prefix to give our threads.
"""
if max_workers is None:
# Use this number because ThreadPoolExecutor is often
# used to overlap I/O instead of CPU work.
max_workers = (cpu_count() or 1) * 5
if max_workers <= 0:
raise ValueError("max_workers must be greater than 0")
self._max_workers = max_workers
self._work_queue = queue.Queue()
self._idle_semaphore = threading.Semaphore(0)
self._threads = set()
self._shutdown = False
self._shutdown_lock = threading.Lock()
self._thread_name_prefix = (thread_name_prefix or
("ThreadPoolExecutor-%d" % self._counter()))
示例3: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self,
num_threads,
sleep=InterruptibleSleep):
"""Constructor for ThreadGate instances.
Args:
num_threads: The total number of threads using this gate.
sleep: Used for dependency injection.
"""
self.__enabled_count = 1
self.__lock = threading.Lock()
self.__thread_semaphore = threading.Semaphore(self.__enabled_count)
self.__num_threads = num_threads
self.__backoff_time = 0
self.__sleep = sleep
示例4: initialize_fallback_semaphores
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def initialize_fallback_semaphores():
"""This needs to be called once at the very beginning of starting ACE."""
# we need some fallback functionality for when the network semaphore server is down
# these semaphores serve that purpose
global_engine_instance_count = saq.CONFIG['global'].getint('global_engine_instance_count')
for key in saq.CONFIG['network_semaphore'].keys():
if key.startswith('semaphore_'):
semaphore_name = key[len('semaphore_'):]
# the configuration settings for the network semaphore specify how many connections
# are allowed to a specific resource at once, globally
# so if we unable to coordinate globally, the fall back is to divide the available
# number of resources between all the engines evenly
# that's what this next equation is for
fallback_limit = int(floor(saq.CONFIG['network_semaphore'].getfloat(key) / float(global_engine_instance_count)))
# we allow a minimum of one per engine
if fallback_limit < 1:
fallback_limit = 1
logging.debug("fallback semaphore count for {0} is {1}".format(semaphore_name, fallback_limit))
fallback_semaphores[semaphore_name] = LoggingSemaphore(fallback_limit)
#fallback_semaphores[semaphore_name] = multiprocessing.Semaphore(fallback_limit)
#fallback_semaphores[semaphore_name].semaphore_name = 'fallback {0}'.format(semaphore_name)
示例5: start
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def start(self):
# Setting up testers
self.setupScanners()
# Setting up threads
self.setupThreads()
self.index = 0
self.dictionary.reset()
self.runningThreadsCount = len(self.threads)
self.running = True
self.playEvent = threading.Event()
self.pausedSemaphore = threading.Semaphore(0)
self.playEvent.clear()
self.exit = False
for thread in self.threads:
thread.start()
self.play()
示例6: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self):
super(WSG50Gripper, self).__init__()
self.max_release = 0
self.sem_list = [Semaphore(value = 0)]
self._status_mutex = Lock()
self._desired_gpos = GRIPPER_OPEN
self._gripper_speed = 300
self._force_counter = 0
self._integrate_gripper_force, self._last_integrate = 0., None
self._last_status_t = time.time()
self.num_timeouts = 0
self.gripper_pub = rospy.Publisher('/wsg_50_driver/goal_position', Cmd, queue_size=10)
rospy.Subscriber("/wsg_50_driver/status", Status, self._gripper_callback)
logging.getLogger('robot_logger').info("waiting for first status")
self.sem_list[0].acquire()
logging.getLogger('robot_logger').info('gripper initialized!')
self._bg = Thread(target=self._background_monitor)
self._bg.start()
示例7: _set_gripper
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def _set_gripper(self, command_pos, wait=False):
self._status_mutex.acquire()
self._desired_gpos = command_pos
if wait:
if self.num_timeouts > MAX_TIMEOUT:
rospy.signal_shutdown("MORE THAN {} GRIPPER TIMEOUTS".format(MAX_TIMEOUT))
sem = Semaphore(value=0) # use of semaphore ensures script will block if gripper dies during execution
self.sem_list.append(sem)
self._status_mutex.release()
start = rospy.get_time()
logging.getLogger('robot_logger').debug("gripper sem acquire, list len-{}".format(len(self.sem_list)))
sem.acquire()
logging.getLogger('robot_logger').debug("waited on gripper for {} seconds".format(rospy.get_time() - start))
else:
self._status_mutex.release()
示例8: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self, topic_data, opencv_tracking=False, save_videos=False):
self._tracking_enabled, self._save_vides = opencv_tracking, save_videos
self._latest_image = LatestObservation(self._tracking_enabled, self._save_vides)
self._is_tracking = False
if self._tracking_enabled:
self.box_height = 80
self.bridge = CvBridge()
self._is_first_status, self._status_sem = True, Semaphore(value=0)
self._cam_height, self._cam_width = None, None
self._last_hash, self._num_repeats = None, 0
if self._save_vides:
self._buffers = []
self._saving = False
self._topic_data = topic_data
self._image_dtype = topic_data.dtype
rospy.Subscriber(topic_data.name, Image_msg, self.store_latest_im)
logging.getLogger('robot_logger').debug('downing sema on topic: {}'.format(topic_data.name))
self._status_sem.acquire()
logging.getLogger('robot_logger').info("Cameras {} subscribed: stream is {}x{}".format(self._topic_data.name, self._cam_width, self._cam_height))
示例9: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self, rtklib_path, config_path):
self.bin_path = rtklib_path
self.config_path = config_path
self.child = 0
self.status = {}
self.obs_rover = {}
self.obs_base = {}
self.info = {}
self.semaphore = Semaphore()
self.started = False
self.launched = False
self.current_config = ""
示例10: set_semaphore
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def set_semaphore(self, asg_meta):
"""
Update no of instances can be terminated based on percentage.
"""
asg_name = asg_meta.get_name()
asg_semaphore = 'semaphore' + asg_name
resp = self._ac_client.describe_auto_scaling_groups(AutoScalingGroupNames=[asg_name])
desired_instances = resp["AutoScalingGroups"][0]["DesiredCapacity"]
if self.terminate_percentage > 100:
self.terminate_percentage = 100
elif self.terminate_percentage <= 0:
self.terminate_percentage = 1
# Get no of instance can parallel be rotated
svalue = int(round(desired_instances * (self.terminate_percentage/100.0)))
if svalue == 0:
svalue = 1
logger.info("Maximum %d instance will be rotated at a time for ASG %s", svalue, asg_name)
asg_semaphore = Semaphore(value=svalue)
return asg_semaphore
示例11: new_job
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def new_job(self, priority, task, inputdata, launcher_name="Unknown", debug=False):
"""
Runs a new job.
It works exactly like the Client class, instead that there is no callback and directly returns result, in the form of a tuple
(result, grade, problems, tests, custom, archive).
"""
job_semaphore = threading.Semaphore(0)
def manage_output(result, grade, problems, tests, custom, state, archive, stdout, stderr):
""" Manages the output of this job """
manage_output.job_return = (result, grade, problems, tests, custom, state, archive, stdout, stderr)
job_semaphore.release()
manage_output.job_return = None
self._client.new_job(priority, task, inputdata, manage_output, launcher_name, debug)
job_semaphore.acquire()
job_return = manage_output.job_return
return job_return
示例12: setUp
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def setUp(self):
"""Set up a TCP server to receive log messages, and a SocketHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
self.server = server = self.server_class(self.address,
self.handle_socket, 0.01)
server.start()
server.ready.wait()
hcls = logging.handlers.SocketHandler
if isinstance(server.server_address, tuple):
self.sock_hdlr = hcls('localhost', server.port)
else:
self.sock_hdlr = hcls(server.server_address, None)
self.log_output = ''
self.root_logger.removeHandler(self.root_logger.handlers[0])
self.root_logger.addHandler(self.sock_hdlr)
self.handled = threading.Semaphore(0)
示例13: createPool
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def createPool(name="main", threads=None, engine=None):
config["POOL_NAME"] = name
try:
threads = int(threads)
except Exception:
threads = config["MAX_THREADS"]
if threads < 2:
threads = 0
engine = engine if engine is not None else config["ENGINE"]
config["MAX_THREADS"] = threads
config["ENGINE"] = engine
config["POOLS"][config["POOL_NAME"]] = {
"pool": Semaphore(threads) if threads > 0 else None,
"engine": Process if "process" in engine.lower() else Thread,
"name": name,
"threads": threads
}
示例14: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self, state: SQLManager, policy, metrics: StatsManager):
if policy not in ('FIFO', 'SIZE', 'DYNSIZE'):
raise UnsupportedSchedulerPolicyError
self.metrics = metrics
self.trigger_semaphore = threading.Semaphore(0)
self.policy = policy
self.queue = []
self.queue_running = []
self.queue_termination = []
self.additional_exec_state = {}
self.loop_quit = False
self.loop_th = threading.Thread(target=self.loop_start_th, name='scheduler')
self.core_limit_recalc_trigger = threading.Event()
self.core_limit_th = threading.Thread(target=self._adjust_core_limits, name='adjust_core_limits')
self.state = state
for execution in self.state.executions.select(status='running'):
if execution.all_services_running:
self.queue_running.append(execution)
else:
self.queue.append(execution)
self.additional_exec_state[execution.id] = ExecutionProgress()
self.loop_th.start()
self.core_limit_th.start()
示例15: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Semaphore [as 别名]
def __init__(self, rtklib_path):
self.bin_path = rtklib_path + "/app/rtkrcv/gcc"
self.config_path = rtklib_path + "/app/rtkrcv"
self.child = 0
self.status = {}
self.obs_rover = {}
self.obs_base = {}
self.info = {}
self.semaphore = Semaphore()
self.started = False
self.launched = False
self.current_config = ""