当前位置: 首页>>代码示例>>Python>>正文


Python Semaphore.get_value方法代码示例

本文整理汇总了Python中multiprocessing.Semaphore.get_value方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.get_value方法的具体用法?Python Semaphore.get_value怎么用?Python Semaphore.get_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在multiprocessing.Semaphore的用法示例。


在下文中一共展示了Semaphore.get_value方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: proc

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import get_value [as 别名]
# -*- coding: utf-8 -*-
__author__ = 'Ljian'
from datetime import datetime
import time
import multiprocessing
from multiprocessing import Semaphore, Process


def proc(sem, interval):
    sem.acquire()
    try:
        print("Current process: %r, acquire, semaphore value: %r"
              % (multiprocessing.current_process().name, sem.get_value()))

        time.sleep(interval)
    finally:
        sem.release()
        print("Current process: %r, release, semaphore value: %r"
              % (multiprocessing.current_process().name, sem.get_value()))

if __name__ == '__main__':
    sem = Semaphore(2)      # 生成个信号量个数为2的,信号量对象
    print(sem.get_value())
    for i in range(4):
        Process(target = proc, args = (sem, i)).start()
开发者ID:ljian1992,项目名称:Demo,代码行数:27,代码来源:Semaphore.py

示例2: Semaphore

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import get_value [as 别名]
#!/usr/bin/python

from multiprocessing import Semaphore

sem = Semaphore()
i = 0
while True:
    i += 1
    if i % 10000 == 0:
        print sem.get_value()

    try:
        sem.release()
    except:
        print `sem.get_value()`
        raise
开发者ID:JedMeister,项目名称:cloudtask,代码行数:18,代码来源:maxsem.py

示例3: set

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import get_value [as 别名]
pids = set()
q = Queue()

# Cream el pool de procesos (pre-fork)
for i in range(min_processos):
    sem_totals.release()
    p = Process(target=run_server, args=(q,))
    p.start()
    pids.add(p.pid)
    # print "Nou proces: ", p.pid

print "\nhttp://%s:%s\n" % (host, port)

while True:
    # controlam amb semàfors que el nombre de processos no surti dels límits establerts
    sem_lliures = sem_totals.get_value() - sem_actius.get_value()
    # ~ print "Lliures:", sem_lliures
    while (sem_totals.get_value() < min_processos) or (
        (sem_totals.get_value() < max_processos) and (sem_lliures < min_processos_lliures)
    ):
        # ~ print "Totals:", sem_totals.get_value()
        sem_totals.release()
        p = Process(target=run_server, args=(q,))  # proces nou
        p.start()
        pids.add(p.pid)
        print "Nou proces:", p.pid

    pid = q.get(True)  # espera
    pids.remove(pid)
    print "Acaba:", pid
开发者ID:vtomasr5,项目名称:adsopy,代码行数:32,代码来源:quechua.py

示例4: SharedExclusiveLock

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import get_value [as 别名]
class SharedExclusiveLock(object):
    def __init__(self, maxreaders=120):
        # Linux max semaphore sets is 120
        self.max = 120
        self._reader = Semaphore(120)
        self._writer = Semaphore(1)
        self._sleeping = Event()

        # Does this process hold the write?
        self.localwrite = False
        self.thread_id = currentThread()

        self.create_methods()

        def after_fork(obj):
            obj._reader._after_fork()
            obj._writer._after_fork()
            obj._sleeping._after_fork()

        register_after_fork(self, after_fork)

    def create_methods(self):
        self.acquire = self._reader.acquire
        self.release = self._reader.release

    @property
    def ismine(self):
        return self.localwrite and (currentThread() == self.thread_id)

    # you can nest write calls
    def wait_noreaders(self):
        if self.ismine:
            return
        while self._reader.get_value() < self.max:
            self._sleeping.set()
            # twiddle the futex
            self._sleeping.wait()

    @contextmanager
    def writing(self):
        if self.ismine:
            yield
        else:
            self.wait_noreaders()
            self._writer.acquire()
            self.localwrite = True
            yield
            self.localwrite = False
            self._writer.release()

    def __enter__(self, blocking=True, timeout=None):
        # prevent writer starvation
        if self.ismine:
            return
        else:
            self._reader.acquire()

    def __exit__(self, *args):
        if self.ismine:
            return
        else:
            self._reader.release()
            if self._sleeping.is_set():
                # twiddle the futex
                self._sleeping.clear()

    def __getstate__(self):
        assert_spawning(self)
        r = self._reader._semlock
        w = self._writer._semlock

        reader = Popen.duplicate_for_child(r.handle), r.kind, r.maxvalue
        writer = Popen.duplicate_for_child(w.handle), w.kind, w.maxvalue
        return (reader, writer)

    def __setstate__(self, state):
        reader, writer = state
        self._reader = SemLock._rebuild(*reader)
        self._writer = SemLock._rebuild(*writer)
开发者ID:sdiehl,项目名称:numpush,代码行数:81,代码来源:shmem_sync.py


注:本文中的multiprocessing.Semaphore.get_value方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。