本文整理汇总了Python中threading.BoundedSemaphore.release方法的典型用法代码示例。如果您正苦于以下问题:Python BoundedSemaphore.release方法的具体用法?Python BoundedSemaphore.release怎么用?Python BoundedSemaphore.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.BoundedSemaphore
的用法示例。
在下文中一共展示了BoundedSemaphore.release方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class CGSBoundedSemaphore:
def __init__(self,value):
self.boundedSemaphore = BoundedSemaphore(value)
self.datasetQueueLock = RLock()
self.datasetQueue = []
def acquire(self,datasetId):
try:
self.datasetQueueLock.acquire()
self.datasetQueue.append(datasetId)
finally:
self.datasetQueueLock.release()
self.boundedSemaphore.acquire()
def release(self,datasetId):
try:
self.datasetQueueLock.acquire()
self.datasetQueue.remove(datasetId)
except:
pass
finally:
self.datasetQueueLock.release()
self.boundedSemaphore.release()
def getIndexDatasetId(self,datasetId):
try:
self.datasetQueueLock.acquire()
return self.datasetQueue.index(datasetId)
except:
return -1
finally:
self.datasetQueueLock.release()
def status(self):
return list(self.datasetQueue)
示例2: __init__
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class ThreadedGroup:
""" An object to keep an arbitary number of workers on an easily-split
task.
"""
def __init__(self):
""" Initialise self """
self._job_count = 0 # The number of active jobs.
self.lock = Lock() # Lock protecting job_count...
self.semaphore = BoundedSemaphore(THREAD_COUNT)
def start(self, func, *args, **kargs):
""" Start the given job. This will block until there are free
workers.
"""
def job_wrapper():
""" Run the job, then decrement the job count and release the
semaphore.
"""
try:
func(*args, **kargs)
finally:
with self.lock:
self._job_count -= 1
self.semaphore.release()
# Create the job.
job = Job(job_wrapper)
# Acquire the semaphore and start.
self.semaphore.acquire()
with self.lock:
self._job_count += 1
job.start()
def wait(self):
""" Wait until all of the jobs are finished """
print("Waiting for jobs to finish...")
while self.get_job_count() != 0:
# Block until another job ends.
self.semaphore.acquire()
print("Jobs finished!")
def get_job_count(self):
""" Return the current job count """
with self.lock:
return self._job_count
示例3: execute_semaphored_threads
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
def execute_semaphored_threads():
inputs = list(range(800, 1000))
print("Calculating from {} to {}".format(inputs[0], inputs[-1]))
# only four threads at time
pool_sema = BoundedSemaphore(value=4)
threads = []
for i in inputs:
# limit threads amount
pool_sema.acquire()
t = Thread(target=execute_fib, args=(i,))
threads.append(t)
t.start()
pool_sema.release()
return threads
示例4: VDOM_semaphore
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class VDOM_semaphore(object):
def __init__(self, counter=1):
self.__semaphore = BoundedSemaphore(counter)
def lock(self):
return self.__semaphore.acquire()
def unlock(self):
self.__semaphore.release()
def __enter__(self):
self.lock()
return self
def __exit__(self, extype, exvalue, traceback):
self.unlock()
示例5: MyThread
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class MyThread(threading.Thread):
def __init__(self,site):
self.site = site
threading.Thread.__init__(self)
self.semaphore = BoundedSemaphore(value=MAXCONN)
self.t = Tweetya()
def run(self):
link = self.t.parse(self.site)
self.semaphore.acquire()
urls = self.t.getlinks()
for i in link:
if not (i in urls):
self.t.setlink(i)
short = self.t.short(i)
title = self.t.gettitle(short)
self.t.auth(str(title)+' '+str(short))
self.semaphore.release()
示例6: IDCardFactory
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class IDCardFactory(Factory):
def __init__(self):
self.owner = []
self.seqNum = 1
self.semaphore = BoundedSemaphore(1)
def createProduct(self, owner):
self.semaphore.acquire()
card = IDCard(self.seqNum, owner)
self.seqNum += 1
self.semaphore.release()
return card
def registerProduct(self, product):
self.owner.append(product.getOwner())
def getOwners(self):
return self.owner
示例7: rpiFIFOClass
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class rpiFIFOClass(deque):
"""
Implements the a Deque with BoundedSemaphore.
Used as a FIFO buffer for the image file names (including the full path).
Stores also the name of the current sub-folder.
"""
def __init__(self, *args):
super(rpiFIFOClass,self).__init__(*args)
self.FIFOSema = BoundedSemaphore()
self.crtSubDir = '/'
self.camID = ''
def acquireSemaphore(self):
self.FIFOSema.acquire()
def releaseSemaphore(self):
self.FIFOSema.release()
def __del__(self):
# self.FIFOSema.release()
self.crtSubDir = ''
示例8: __init__
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class Results:
def __init__(self):
self.mutex = BoundedSemaphore(value=1)
self.__issues = []
def __len__(self):
return len(self.__issues)
# ------------------------------------------------
# data functions
# ------------------------------------------------
def add_result(self, result):
self.mutex.acquire()
self.__issues.append(result)
self.mutex.release()
def get_result(self, index):
self.mutex.acquire()
item = self.__issues[index]
self.mutex.release()
return item
示例9: BlockingRequest
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class BlockingRequest():
def __init__(self):
self.semaphore = BoundedSemaphore(1)
self.exception = None
self.response = None
self.semaphore.acquire(True)
def response_callback(self, response):
self.response = response
self.semaphore.release()
def error_callback(self, exception):
self.exception = exception
self.semaphore.release()
''' returns the response or throws an exception '''
def await_response(self):
self.semaphore.acquire(True)
if self.exception :
raise self.exception
return self.response
示例10: semaphore
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class semaphore(object):
""" Class encapsulating a semaphore to limit
number of resources """
def __init__(self, *args):
self.sem = BoundedSemaphore(args[0])
def __call__(self, f):
def semfunc(*args, **kwargs):
try:
print 'Trying to acquire sem=>',currentThread()
self.sem.acquire()
print 'Acquired sem=>',currentThread()
try:
return f(*args, **kwargs)
except Exception, e:
raise
finally:
self.sem.release()
print 'Released sem=>',currentThread()
return semfunc
示例11: BuildDispatcher
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class BuildDispatcher(Singleton):
def _singleton_init(self):
self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
self.builders = dict()
self.builders_lock = BoundedSemaphore()
NotificationCenter().add_observer(self, 'handle_state_change', 'image.status')
def handle_state_change(self, notification):
status = notification.user_info['new_status']
if(status in ('COMPLETED', 'FAILED', 'DELETED', 'DELETEFAILED')):
self.builders_lock.acquire()
image_id = notification.sender.identifier
if(image_id in self.builders):
del self.builders[image_id]
self.log.debug('Removed builder from BuildDispatcher on notification from image %s: %s' % (image_id, status))
self.builders_lock.release()
def builder_for_base_image(self, template, parameters=None):
builder = Builder()
builder.build_image_from_template(template, parameters=parameters)
self.builders_lock.acquire()
try:
self.builders[builder.base_image.identifier] = builder
finally:
self.builders_lock.release()
return builder
def builder_for_target_image(self, target, image_id=None, template=None, parameters=None):
builder = Builder()
builder.customize_image_for_target(target, image_id, template, parameters)
self.builders_lock.acquire()
try:
self.builders[builder.target_image.identifier] = builder
finally:
self.builders_lock.release()
return builder
def builder_for_provider_image(self, provider, credentials, target, image_id=None, template=None, parameters=None):
builder = Builder()
builder.create_image_on_provider(provider, credentials, target, image_id, template, parameters)
self.builders_lock.acquire()
try:
self.builders[builder.provider_image.identifier] = builder
finally:
self.builders_lock.release()
return builder
示例12: Store
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
class Store(object):
def __init__(self, item_number, person_capacity):
if type(item_number) is not int:
raise Exception("item_number is not an integer")
if type(person_capacity) is not int:
raise Exception("person_capacity is not an integer")
self.item_number = item_number
self.person_capacity = person_capacity
self.sema = BoundedSemaphore(value=self.person_capacity)
def enter(self):
self.sema.acquire()
def buy(self):
sleep(randint(5, 10))
if self.item_number:
purchase = True
self.item_number -= 1
else:
purchase = False
self.sema.release()
return purchase
示例13: map_task
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
def map_task(source, func, thread_limit=10):
'''Run func in up to thread_limit threads, with data from
source arg passed into it.
The arg source must be iterable. map_task() will call next()
each time a free thread is available.
The function will block until all of the tasks are completed.
'''
assert thread_limit > 0
e = BoundedSemaphore(thread_limit)
task_list = []
for i in xrange(0, thread_limit):
task_list.append(task(func, e))
iterer = source.__iter__()
data = None
while 1:
try:
if data is None:
data = iterer.next()
t = get_free_task(task_list)
if t:
t.data = data
t.start()
data = None
else:
# print >> sys.stderr, 'waiting for e'
e.acquire()
e.release()
except StopIteration:
# iteration is stopped
# print >>sys.stderr, 'terminating'
for a_task in task_list:
# print >>sys.stderr, 'terminating ' + str(a_task)
a_task.terminate_and_join()
return
示例14: growSortC
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
def growSortC():
semaphore = BoundedSemaphore()
semaphore.acquire()
global_var.counter4 += 1
semaphore.release()
示例15: growSortT
# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import release [as 别名]
def growSortT(tSum):
semaphore = BoundedSemaphore()
semaphore.acquire()
global_var.timer4 += tSum
semaphore.release()