本文整理汇总了Python中threading.Semaphore.acquire方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.acquire方法的具体用法?Python Semaphore.acquire怎么用?Python Semaphore.acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Semaphore
的用法示例。
在下文中一共展示了Semaphore.acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class StatisticQueue:
def __init__(self, stats):
self._semaphore = Semaphore()
self.result = {}
self.stats = stats
def write_result(self, data):
self._semaphore.acquire()
self.result.update(data)
self._semaphore.release()
def start_parse(self):
self.stats.connect()
self.stats.init_message_stack()
func_to_start = [
self.stats.get_top3_speakers,
self.stats.get_most_frequent_youtube_video,
self.stats.get_time_activity,
self.stats.get_abusive_expressions,
]
threads = []
for func in func_to_start:
thread = Thread(target=func, args=(self, ))
threads.append(thread)
thread.start()
for t in threads:
t.join()
return self.result
示例2: _setup_to_do_n_cycles
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
def _setup_to_do_n_cycles(self, number_of_cycles: int, updates_each_cycle: UpdateCollection=None):
"""
Sets up the test so that the retriever will only do n cycles.
:param number_of_cycles: the number of cycles to do
"""
if updates_each_cycle is None:
updates_each_cycle = UpdateCollection([])
semaphore = Semaphore(0)
lock_until_counted = Lock()
lock_until_counted.acquire()
def increase_counter(*args) -> UpdateCollection:
semaphore.release()
lock_until_counted.acquire()
return updates_each_cycle
self.retrieval_manager.update_mapper.get_all_since.side_effect = increase_counter
self.retrieval_manager.start()
run_counter = 0
while run_counter < number_of_cycles:
semaphore.acquire()
run_counter += 1
lock_until_counted.release()
if run_counter == number_of_cycles:
self.retrieval_manager.stop()
self.retrieval_manager.update_mapper.get_all_since.side_effect = None
示例3: ReusableBarrierSem
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class ReusableBarrierSem():
def __init__(self, num_threads):
self.num_threads = num_threads
self.count_threads1 = self.num_threads
self.count_threads2 = self.num_threads
self.counter_lock = Lock() # protejam decrementarea numarului de threaduri
self.threads_sem1 = Semaphore(0) # contorizam numarul de threaduri pentru prima etapa
self.threads_sem2 = Semaphore(0) # contorizam numarul de threaduri pentru a doua etapa
def wait(self):
self.phase1()
self.phase2()
def phase1(self):
with self.counter_lock:
self.count_threads1 -= 1
if self.count_threads1 == 0:
for i in range(self.num_threads):
self.threads_sem1.release()
self.count_threads2 = self.num_threads
self.threads_sem1.acquire()
def phase2(self):
with self.counter_lock:
self.count_threads2 -= 1
if self.count_threads2 == 0:
for i in range(self.num_threads):
self.threads_sem2.release()
self.count_threads1 = self.num_threads
self.threads_sem2.acquire()
示例4: my_scan
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
def my_scan(myip,myemail):
global manager
Mydir="/root/myopenvas/results/"+myemail[0:myemail.find('@')]
if os.path.isfile(Mydir+"/"+myip+".html"):
print myip+" already exist"
return
start=datetime.datetime.now()
print "Start of: "+myip+" at : ",start
# """
Sem =Semaphore(0)
scan_id,target_id=manager.launch_scan(
target=myip,
profile="Full and fast",
callback_end=partial(lambda x:x.release(),Sem),
callback_progress=my_print_status
)
Sem.acquire()
# """
end=datetime.datetime.now()
print "End of: "+myip+" at : ",end
print "*******************************"
print "Cost :",(end-start)
print "*******************************"
report_id=manager.get_tasks_last_report_id(scan_id)
write_report(report_id,myip,myemail)
示例5: ObjKeeper
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class ObjKeeper(object):
"""
每种资源
"""
def __init__(self, max_size):
self.lock = Semaphore(max_size)
self.objs = deque()
def pop(self):
# 获取锁
self.lock.acquire()
try:
return self.objs.popleft()
except:
# 代表外面要重新生成新的
return None
def push(self, obj):
if obj:
self.objs.append(obj)
# 无论如何都要释放
self.lock.release()
示例6: __init__
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class Synchronized:
def __init__(self):
from threading import Semaphore
self.__lock = Semaphore()
self.__ownerThread = None
classdict = self.__class__.__dict__
for attr in classdict.get("__synchronized__", ()):
try:
method = classdict[attr]
if callable(method):
self.__dict__[attr] = CallHook(self, method)
else:
if VERBOSE: print "! Synchronized: Object is not callable: %s" % (attr,)
except KeyError:
if VERBOSE: print "! Synchronized: Method not found: %s" % (attr,)
def releaseInstance(self):
self.__ownerThread = None
self.__lock.release()
def acquireInstance(self):
self.__lock.acquire()
self.__ownerThread = currentThread()
def ownerThread(self):
return self.__ownerThread
示例7: __init__
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class Race:
def __init__(self):
self.stageFinishCount = 0
self.stageStartCount = 0
self.mutex = Semaphore(1) #used for mutual exclusion while writing
self.stageFinishSema = Semaphore(0)
self.stageStartSema = Semaphore(0) #This is used so that people dont try to finish the next stage until everyone has left the prev stage
def teammate_start_stage(self):
count = 0
with self.mutex:
self.stageStartCount = self.stageStartCount + 1
count = self.stageStartCount
if count < NUM_TEAMMATES:
self.stageStartSema.acquire()
else:
self.stageStartCount = 0
for i in range(NUM_TEAMMATES-1):
self.stageStartSema.release() # only last person starting the stage would release all thread.
def teammate_finish_stage(self):
count = 0 #local variable separate to each thread
with self.mutex:
self.stageFinishCount = self.stageFinishCount + 1
count = self.stageFinishCount
if count < NUM_TEAMMATES:
self.stageFinishSema.acquire()
else:
self.stageFinishCount = 0
for i in range(NUM_TEAMMATES-1):
self.stageFinishSema.release() #last teammate only can do this
示例8: CollapseTransition
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class CollapseTransition(XiboTransition):
def run(self):
self.lock = Semaphore()
self.lock.acquire()
# Only valid as an exit transition
if self.media1 != None:
if self.options1['transOutDuration'] > 0:
self.outDuration = int(self.options1['transOutDuration'])
else:
self.outDuration = 1000
self.__animate__(self.media1.getName(),self.media1.getX(), self.media1.getY(),self.media1.getWidth(),self.media1.getHeight(),self.outDuration,self.next)
self.lock.acquire()
self.callback()
def next(self):
self.lock.release()
def __animate__(self,name,currentX,currentY,w,h,duration,callback):
# ('ease', nodeName, animation duration, animation attribute, start position, finish position, callback on Stop, easeIn duration, easeOut duration)
self.log.log(5,"info","CollapseTransition: Collapsing " + name + " over " + str(duration) + "ms")
self.p.enqueue('anim',('linear',name,duration,'y',currentY,int(h/2),None))
self.p.enqueue('anim',('linear',name,duration,'height',int(h),0,callback))
self.p.enqueue('timer',(duration,self.next))
示例9: _Result
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class _Result(object):
'''
Class for a result composed of partials.
Partials are stored in a dict:
```
{
start_index: chunk,
...
}
```
To get the result, a call to `get` method must be done.
The call to `get` is blocking until the result is `completed`.
Everytime a partial is added using the method
`add_partial`, the count of chunks is incremented and
compared with the expected number of chunks (given on `__init__`).
If the count is equal, then a semaphore is released.
The same semaphore is acquired on `get` call.
That's why `get` call is blocking.
'''
def __init__(self, no_chunks):
super(_Result, self).__init__()
self._n = no_chunks
self._res = {}
self._lock = Lock()
self._no_collected = 0
self._completed_sem = Semaphore(0)
@property
def completed(self):
self._lock.acquire()
comp = self._no_collected == self._n
self._lock.release()
return comp
# no setter provided
@property
def partial(self):
return self._res
def add_partial(self, key, partial):
# thread-safe
self._lock.acquire()
self._res[key] = partial
self._no_collected += 1
self._lock.release()
if self.completed:
self._completed_sem.release()
def get(self):
self._completed_sem.acquire()
# release the semaphore for further
# invokations of get()
self._completed_sem.release()
return self._res
示例10: JobManager
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class JobManager(LogMaster):
"""
keeps a queue of jobs, and runs them in a separate thread, while keeping the number of worker thread under
a specified treshold.
it is not a real thread pool as new thread are fired every time
"""
def __init__(self, maxthreads, loglevel=logging.INFO):
self.setLogger(self.__class__.__name__, loglevel)
self.maxthreads = maxthreads
self.semaph = Semaphore(value=self.maxthreads)
self.jobq = Queue()
self.running = True
self.dispatcher = Thread(target=self._jobdispatcher, daemon=True)
self.dispatcher.start()
def putjob(self, job):
self.jobq.put(job)
def harness(self, job):
job.execute()
self.semaph.release()
def _jobdispatcher(self):
self.logger.debug("Started job dispatcher thread")
while self.running:
self.semaph.acquire()
job = self.jobq.get()
if job is None:
self.semaph.release()
continue
t = Thread(target=self.harness, args=(job,), daemon=True)
t.start()
self.logger.debug("Stopped job dispatcher thread")
示例11: recover_images
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
def recover_images(parser, destination):
"""Parse images and save them to <manga>/<chapter>/<image>."""
urls = parser.parse()
manga_path = os.path.join(destination, parser.title)
ch_digits = len(str(len(urls)))
for chapter, pages in urls:
#Normalize chapter digits
chapter = "0" * (ch_digits - len(str(chapter))) + str(chapter)
chapter_path = os.path.join(manga_path, chapter)
if not os.path.exists(chapter_path):
os.makedirs(chapter_path)
savers = list()
logging.info('Saving Chapter %s to %s', chapter, chapter_path)
pg_digits = len(str(len(pages)))
sem = Semaphore(BaseParser.MAX_CONNECTIONS)
for page, url in enumerate(pages, start=1):
sem.acquire()
#Normalize page digits
page = "0" * (pg_digits - len(str(page))) + str(page)
path = os.path.join(chapter_path, str(page) + '.jpg')
saver = utils.ImageSaver(path, url, sem)
savers.append(saver)
saver.start()
map(lambda thread: thread.join(), savers)
示例12: __init__
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class BarberShop:
def __init__(self, num_chairs):
self.ncustomersWaiting = Semaphore(0)
self.semaChairs = Semaphore(num_chairs)# number of vacant chairs in barber shop
self.nbarbers = Semaphore(0)
# check for waiting customers
# if there are none, wait
# if there are waiting customers, signal one
def barber_ready_to_cut(self):
self.ncustomersWaiting.acquire()
self.nbarbers.release()
# enter the barbershop if all num_chairs are not occupied
# returns true if the customer entered successfully, and
# false if he was turned away at the door
def customer_enter(self):
if self.semaChairs.acquire(False) :
return True
else :
return False
# take a seat and wait until the barber is ready to cut hair
def customer_take_a_seat(self):
self.ncustomersWaiting.release() # Since release is done first, deadlock won't occur with barber
self.nbarbers.acquire()
self.semaChairs.release()
示例13: PromptService
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class PromptService(object):
def __init__(self):
self.semaphore = Semaphore(0)
self.commandWindow = None
self.response = None
def setCommandWindow(self, window):
self.commandWindow = window
def requestInput(self, prompt):
if self.commandWindow is None:
raise RuntimeError("Command window hasn't registered itself")
if prompt is None:
prompt = ''
self.commandWindow.prompt(prompt, 'standard-output', self.respond, 'standard-input')
self.semaphore.acquire()
if self.response is None:
raise KeyboardInterrupt
else:
res = self.response
self.response = None
return str(res)
def respond(self, value):
self.response = value
self.semaphore.release()
示例14: TestPubSubscribe
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
class TestPubSubscribe(unittest.TestCase):
def onMessage(self, message):
self.assertTrue(len(message.payload_objects) > 0)
msg_body = message.payload_objects[0].content
self.assertIn(msg_body, MESSAGES)
self.counter += 1
if self.counter == len(MESSAGES):
self.semaphore.release()
def setUp(self):
self.counter = 0
self.semaphore = Semaphore(0)
self.bw_client = Client()
self.bw_client.setEntityFromFile(KEY_FILE)
self.bw_client.overrideAutoChainTo(True)
self.bw_client.subscribe(URI, self.onMessage)
def tearDown(self):
self.bw_client.close()
def testPublishSubscribe(self):
for msg in MESSAGES:
po = PayloadObject((64, 0, 0, 0), None, msg)
self.bw_client.publish(URI, payload_objects=(po,))
self.semaphore.acquire()
示例15: test_group_action
# 需要导入模块: from threading import Semaphore [as 别名]
# 或者: from threading.Semaphore import acquire [as 别名]
def test_group_action(self):
start = time.time()
semaphore = Semaphore(0)
controller = self._get_controller()
controller.set_unittest_semaphore(semaphore)
# New controller is empty
self.assertEquals(len(controller.schedules), 0)
with self.assertRaises(RuntimeError) as ctx:
# Doesn't support duration
controller.add_schedule('group_action', start + 120, 'GROUP_ACTION', None, None, 1000, None)
self.assertEquals(ctx.exception.message, 'A schedule of type GROUP_ACTION does not have a duration. It is a one-time trigger')
with self.assertRaises(RuntimeError) as ctx:
# Incorrect argument
controller.add_schedule('group_action', start + 120, 'GROUP_ACTION', 'foo', None, None, None)
self.assertEquals(ctx.exception.message, 'The arguments of a GROUP_ACTION schedule must be an integer, representing the Group Action to be executed')
controller.add_schedule('group_action', start + 120, 'GROUP_ACTION', 1, None, None, None)
self.assertEquals(len(controller.schedules), 1)
self.assertEquals(controller.schedules[0].name, 'group_action')
self.assertEquals(controller.schedules[0].status, 'ACTIVE')
controller.start()
semaphore.acquire()
self.assertEquals(GatewayApi.RETURN_DATA['do_group_action'], 1)
self.assertEquals(len(controller.schedules), 1)
self.assertEquals(controller.schedules[0].name, 'group_action')
self.assertEquals(controller.schedules[0].status, 'COMPLETED')
controller.stop()