python类Semaphore()的实例源码

lockutils.py 文件源码 项目:weibo 作者: windskyer 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def get(self, name):
        """Gets (or creates) a semaphore with a given name.

        :param name: The semaphore name to get/create (used to associate
                     previously created names with the same semaphore).

        Returns an newly constructed semaphore (or an existing one if it was
        already created for the given name).
        """
        with self._lock:
            try:
                return self._semaphores[name]
            except KeyError:
                sem = threading.Semaphore()
                self._semaphores[name] = sem
                return sem
ui_bridge.py 文件源码 项目:python-gui 作者: neovim 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def connect(self, nvim, ui, profile=None, notify=False):
        """Connect nvim and the ui.

        This will start loops for handling the UI and nvim events while
        also synchronizing both.
        """
        self._notify = notify
        self._error = None
        self._nvim = nvim
        self._ui = ui
        self._profile = profile
        self._sem = Semaphore(0)
        self.debug_events = len(os.environ.get("NVIM_PYTHON_UI_DEBUG", "")) > 0
        t = Thread(target=self._nvim_event_loop)
        t.daemon = True
        t.start()
        self._ui_event_loop()
        if self._error:
            print(self._error)
        if self._profile:
            print(self._profile)
adaptive_thread_pool.py 文件源码 项目:Intranet-Penetration 作者: yuxiaokui 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
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
adaptive_thread_pool.py 文件源码 项目:MKFQ 作者: maojingios 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
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
cluster_provider.py 文件源码 项目:cc-server 作者: curious-containers 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __init__(self, config, tee, node_name, node_config):
        self._config = config
        self._tee = tee

        self.node_name = node_name
        self.node_config = node_config

        self._thread_limit = Semaphore(self._config.docker['thread_limit'])

        tls = False
        if self.node_config.get('tls'):
            tls = docker.tls.TLSConfig(**self.node_config['tls'])

        try:
            client_class = docker.APIClient
        except AttributeError:
            client_class = docker.Client
            self._tee('Node {}: Fallback to old docker-py Client.'.format(self.node_name))

        self.client = client_class(
            base_url=self.node_config['base_url'],
            tls=tls,
            timeout=self._config.docker.get('api_timeout'),
            version='auto'
        )
connection.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 converter=None, validate_certs=True, anon=False,
                 security_token=None, profile_name=None):
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint,
                                connection_cls=STSConnection)
        self.region = region
        self.anon = anon
        self._mutex = threading.Semaphore()
        super(STSConnection, self).__init__(aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    validate_certs=validate_certs,
                                    security_token=security_token,
                                    profile_name=profile_name)
test_activity.py 文件源码 项目:urban-journey 作者: urbanjourney 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_direct_call(self):
        """Calls the activity directly."""
        """Creates one trigger and an activity and triggers it."""
        foo = TriggerBase()
        bas = [None]
        s = Semaphore(0)

        @activity(foo)
        async def bar():
            bas[0] = "Triggered"
            s.release()

        asyncio.run_coroutine_threadsafe(bar(), self.loop)
        s.acquire()

        self.assertEqual(bas[0], "Triggered")

    # Activities outside of modules where only meant to be used during early stages of development. The are officially
    # not supported.
test_activity.py 文件源码 项目:urban-journey 作者: urbanjourney 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_parameters(self):
        """Triggers an activity and passes extra parameters."""
        bas = [None]
        foo = TriggerBase()

        s = Semaphore(0)

        @activity(foo, "arg", k="kwarg")
        async def bar(p, k):
            bas[0] = p + k
            s.release()

        asyncio.run_coroutine_threadsafe(foo.trigger(), self.loop)
        assert s.acquire(timeout=0.1)

        self.assertEqual(bas[0], "argkwarg")
test_trigger.py 文件源码 项目:urban-journey 作者: urbanjourney 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def test_simple_descriptor_trigger(self):

        class Foo(ModuleBase):
            def __init__(self, s):
                super().__init__()
                self.bar = None
                self.s = s

            trigger = DescriptorClassTrigger(TriggerBase)

            @activity(trigger)
            async def activity(self):
                self.bar = "qwertyuiop"
                s.release()

        s = Semaphore(0)
        foo = Foo(s)
        asyncio.run_coroutine_threadsafe(foo.trigger.trigger(), self.loop)
        self.assertTrue(s.acquire(timeout=0.1))

        self.assertEqual(foo.bar, "qwertyuiop")
test_clock.py 文件源码 项目:urban-journey 作者: urbanjourney 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_clock(self):
        bas = [0]
        clk = Clock(100)

        s = Semaphore(0)

        @activity(clk)
        async def foo():
            bas[0] += 1
            if bas[0] >= 5:
                clk.stop()
                s.release()

        t0 = time()
        clk.start()
        self.assertTrue(s.acquire(timeout=0.1))
        self.assertGreaterEqual(time() - t0, 0.05)
        self.assertEqual(bas[0], 5)
ui_bridge.py 文件源码 项目:pytknvim 作者: timeyyy 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def connect(self, nvim, ui, profile=None, notify=False):
        """Connect nvim and the ui.

        This will start loops for handling the UI and nvim events while
        also synchronizing both.
        """
        self._notify = notify
        self._error = None
        self._nvim = nvim
        self._ui = ui
        self._profile = profile
        self._sem = Semaphore(0)
        t = Thread(target=self._nvim_event_loop)
        t.daemon = True
        t.start()
        self._ui_event_loop()
        if self._error:
            print(self._error)
        if self._profile:
            print(self._profile)
ngamsHighLevelLib.py 文件源码 项目:ngas 作者: ICRAR 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def releaseDiskResource(ngamsCfgObj,
                        slotId):
    """
    Release a disk resource acquired with
    ngamsHighLevelLib.acquireDiskResource().

    ngamsCfgObj:   NG/AMS Configuration Object (ngamsConfig).

    slotId:        Slot ID referring to the disk resource (string).

    Returns:       Void.
    """
    T = TRACE()

    storageSet = ngamsCfgObj.getStorageSetFromSlotId(slotId)
    if (not storageSet.getMutex()): return

    global _diskMutexSems
    if (not _diskMutexSems.has_key(slotId)):
        _diskMutexSems[slotId] = threading.Semaphore(1)
    logger.debug("Releasing disk resource with Slot ID: %s", slotId)
    _diskMutexSems[slotId].release()
dataset.py 文件源码 项目:kge-server 作者: vfrico 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(self, sparql_endpoint=None, thread_limiter=4):
        """Creates the dataset class

        The default endpoint is the original from wikidata.

        :param string sparql_endpoint: The URI of the SPARQL endpoint
        :param integer thread_limiter: The number of concurrent HTTP queries
        """
        if sparql_endpoint is not None:
            self.SPARQL_ENDPOINT = sparql_endpoint

        self.th_semaphore = threading.Semaphore(thread_limiter)
        # self.query_sem = threading.Semaphore(thread_limiter)

        # Instanciate splited subs as false
        self.splited_subs = {'updated': False}
action.py 文件源码 项目:pioneer 作者: NSO-developer 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __init__(self, *args, **kwds):
        # Setup the NCS object, containing mechanisms
        # for communicating between NCS and this User code.
        self._ncs = NcsPyVM(*args, **kwds)

        # Just checking if the NCS logging works...
        self.debug('Initalizing object')

        # Register our 'finish' callback
        self._finish_cb = lambda: self.finish()
        self._ncs.reg_finish(self._finish_cb)
        self.mypipe = os.pipe()

        self.waithere = threading.Semaphore(0)  # Create as blocked

    # This method starts the user application in a thread
adaptive_thread_pool.py 文件源码 项目:xxNet 作者: drzorm 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
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
PLCObject.py 文件源码 项目:beremiz 作者: nucleron 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def StartPLC(self):
        if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
            c_argv = ctypes.c_char_p * len(self.argv)
            error = None
            res = self._startPLC(len(self.argv), c_argv(*self.argv))
            if res == 0:
                self.PLCStatus = "Started"
                self.StatusChange()
                self.PythonRuntimeCall("start")
                self.StartSem = Semaphore(0)
                self.PythonThread = Thread(target=self.PythonThreadProc)
                self.PythonThread.start()
                self.StartSem.acquire()
                self.LogMessage("PLC started")
            else:
                self.LogMessage(0, _("Problem starting PLC : error %d" % res))
                self.PLCStatus = "Broken"
                self.StatusChange()
test_logging.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
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)
ip.py 文件源码 项目:pknx 作者: open-homeautomation 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __init__(self, ip="0.0.0.0", port=3671, valueCache=None):
        """Initialize the connection to the given host/port

        Initialized the connection, but does not connect.
        """
        self.remote_ip = ip
        self.remote_port = port
        self.discovery_port = None
        self.data_port = None
        self.connected = False
        self.result_queue = queue.Queue()
        self.ack_semaphore = threading.Semaphore(0)
        self.conn_state_ack_semaphore = threading.Semaphore(0)
        if valueCache is None:
            self.value_cache = ValueCache()
        else:
            self.value_cache = valueCache
        self.connection_state = 0
        self.keepalive_thread = threading.Thread(target=self.keepalive,
                                                 args=())
        self.keepalive_thread.daemon = True
        self.keepalive_thread.start()
        self._lock = threading.Lock()
        self._write_delay = 0.05
connection.py 文件源码 项目:Chromium_DepotTools 作者: p07r0457 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 converter=None, validate_certs=True):
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint,
                                connection_cls=STSConnection)
        self.region = region
        self._mutex = threading.Semaphore()
        AWSQueryConnection.__init__(self, aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    validate_certs=validate_certs)
connection.py 文件源码 项目:node-gn 作者: Shouqun 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 converter=None, validate_certs=True):
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint,
                                connection_cls=STSConnection)
        self.region = region
        self._mutex = threading.Semaphore()
        AWSQueryConnection.__init__(self, aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    validate_certs=validate_certs)
adaptive_thread_pool.py 文件源码 项目:Deploy_XXNET_Server 作者: jzp820927 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
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
wiki_trainer.py 文件源码 项目:Image_Retrieval 作者: ddlricardo 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def myrunner(func):
    sem = td.Semaphore(config.num_thread)

    def wrapper(i):
        sem.acquire()
        try:
            func(i)
        except Exception as e:
            raise
        finally:
            sem.release()

    ts = []
    for i in range(10):
        t = td.Thread(target=wrapper, args=(i,))
        t.start()
        ts.append(t)

    for t in ts: t.join()
nus_trainer.py 文件源码 项目:Image_Retrieval 作者: ddlricardo 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def myrunner(func):
    sem = td.Semaphore(config.num_thread)

    def wrapper(i):
        sem.acquire()
        try:
            func(i)
        except Exception as e:
            raise
        finally:
            sem.release()

    ts = []
    for i in range(10):
        t = td.Thread(target=wrapper, args=(i,))
        t.start()
        ts.append(t)

    for t in ts: t.join()
test_logging.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
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)
adaptive_thread_pool.py 文件源码 项目:Docker-XX-Net 作者: kuanghy 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
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
connection.py 文件源码 项目:depot_tools 作者: webrtc-uwp 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 converter=None, validate_certs=True):
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint,
                                connection_cls=STSConnection)
        self.region = region
        self._mutex = threading.Semaphore()
        AWSQueryConnection.__init__(self, aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    validate_certs=validate_certs)
tv_grab_fetch.py 文件源码 项目:tvgrabpyAPI 作者: tvgrabbers 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, config):
        self.config = config
        self.max_fetches = Semaphore(self.config.opt_dict['max_simultaneous_fetches'])
        self.count_lock = RLock()
        self.progress_counter = 0
        self.channel_counters = {}
        self.source_counters = {}
        self.source_counters['total'] = {}
        self.raw_json = {}
        self.cache_id = self.config.cache_id
        self.json_id = self.config.json_id
        self.ttvdb1_id = self.config.ttvdb1_id
        self.ttvdb2_id = self.config.ttvdb2_id
        self.imdb3_id = self.config.imdb3_id

    # end init()
caching.py 文件源码 项目:mumblecode 作者: mumbleskates 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __init__(self, session, cache, heuristic, transform=None, limiter=None, max_inflight=0):
        """
        :param session: requests session to use

        :param cache: cache to use

        :param heuristic: function that accepts a partially constructed Response object (with only
          `expiry` set to `None`) and returns the number of seconds this data will be fresh for.

        :param transform: function that accepts a partially constructed Response object (with `expiry` and
          `transformed` still set to `None`) and returns any object to represent this data, which may be used
          to determine the result's lifetime

        :param limiter: This object is called once every time the network is accessed. Any returned data is discarded.

        """
        self.session = session
        self.cache = cache
        self.heuristic = heuristic
        self.transform = transform or (lambda x: None)
        self.limiter = limiter or (lambda: None)
        if max_inflight > 0:
            self.inflight = Semaphore(max_inflight)
        else:
            self.inflight = None
common.py 文件源码 项目:chirp_fork 作者: mach327 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __init__(self, radio, parent=None):
        threading.Thread.__init__(self)
        gobject.GObject.__init__(self)
        self.__queue = {}
        if parent:
            self.__runlock = parent._get_run_lock()
            self.status = lambda msg: parent.status(msg)
        else:
            self.__runlock = threading.Lock()
            self.status = self._status

        self.__counter = threading.Semaphore(0)
        self.__lock = threading.Lock()

        self.__enabled = True
        self.radio = radio
pool.py 文件源码 项目:logscan 作者: magedu 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __init__(self, size):
        self.__pool = threading.Semaphore(size)
        self.__threads = []


问题


面经


文章

微信
公众号

扫码关注公众号