python类BoundedSemaphore()的实例源码

worker.py 文件源码 项目:OpenDoor 作者: stanislav-web 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, queue, num_threads, timeout=0):
        """
        Init thread worker
        :param Queue.Queue queue: simple queue object
        :param int num_threads: threads numbers
        :param int timeout: delay timeout
        """

        super(Worker, self).__init__()
        self.__semaphore = BoundedSemaphore(num_threads)
        self.__event = Event()
        self.__event.set()
        self.__empty = False
        self.__running = True
        self.__queue = queue
        self.__timeout = timeout
        self.counter = 0
oneshellcrack.py 文件源码 项目:oneshellcrack 作者: L-codes 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def main():
    global semaphore, sends

    signal.signal(signal.SIGINT, interrupt_handler)
    args = commandline()
    print(' ( Shell:{shell}, Numbers:{max_request}, Threads:{max_threads}, Retry:{max_retry} )\n'.format(**args.__dict__))

    semaphore = BoundedSemaphore(value=args.max_threads)
    stopwatch_start = time.time()
    for i, payload in enumerate(create_payload(args), 1):
        if attack:
            sends = i
            semaphore.acquire()
            t = Thread(target=crack, args=(i, args, payload))
            t.setDaemon(True)
            t.start()

    for _ in range(args.max_threads):
        semaphore.acquire()

    stopwatch = time.time() - stopwatch_start
    words = args.max_request * sends if sends else pwd_total
    speed = words / stopwatch if stopwatch else 0
    msg = '[Success] Password: {}'.format(pwd) if pwd else '[Failed] No password found'
    print('\n\n{msg}\n[Finish] {words} words in {stopwatch:.3f} seconds. ({speed:.0f} w/s)'.format(**locals()))
test_threading.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_BoundedSemaphore_limit(self):
        # BoundedSemaphore should raise ValueError if released too often.
        for limit in range(1, 10):
            bs = threading.BoundedSemaphore(limit)
            threads = [threading.Thread(target=bs.acquire)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            threads = [threading.Thread(target=bs.release)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            self.assertRaises(ValueError, bs.release)
test_threading.py 文件源码 项目:python2-tracer 作者: extremecoders-re 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_BoundedSemaphore_limit(self):
        # BoundedSemaphore should raise ValueError if released too often.
        for limit in range(1, 10):
            bs = threading.BoundedSemaphore(limit)
            threads = [threading.Thread(target=bs.acquire)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            threads = [threading.Thread(target=bs.release)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            self.assertRaises(ValueError, bs.release)
postag.py 文件源码 项目:senti 作者: stevenxxiu 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def transform(self, docs, buffer_size=100):
        args = shlex.split(self.RUN_TAGGER_CMD) + ['--output-format', 'conll']
        proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        buffer_sema = threading.BoundedSemaphore(buffer_size)
        t = threading.Thread(target=self._write_input, args=(docs, proc, buffer_sema))
        t.start()
        while True:
            # reading can only follow writing unless EOF is reached so buffer_sema >= 0
            res = []
            while True:
                line = proc.stdout.readline().decode('utf-8').rstrip()
                if line == '':
                    break
                word, tag, confidence = line.split('\t')
                res.append((word, tag, float(confidence)))
            if not res:
                break
            yield res
            buffer_sema.release()
        t.join()
test_threading.py 文件源码 项目:web_ctp 作者: molebot 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_BoundedSemaphore_limit(self):
        # BoundedSemaphore should raise ValueError if released too often.
        for limit in range(1, 10):
            bs = threading.BoundedSemaphore(limit)
            threads = [threading.Thread(target=bs.acquire)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            threads = [threading.Thread(target=bs.release)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            self.assertRaises(ValueError, bs.release)
test_threading.py 文件源码 项目:pefile.pypy 作者: cloudtracer 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_BoundedSemaphore_limit(self):
        # BoundedSemaphore should raise ValueError if released too often.
        for limit in range(1, 10):
            bs = threading.BoundedSemaphore(limit)
            threads = [threading.Thread(target=bs.acquire)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            threads = [threading.Thread(target=bs.release)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            self.assertRaises(ValueError, bs.release)
gce_firewall_enforcer_test.py 文件源码 项目:forseti-security 作者: GoogleCloudPlatform 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def setUp(self):
        """Set up.

        Creates a FirewallEnforcer object with current and expected rules set to
        an empty FirewallRules object.
        """
        self.gce_service = mock.MagicMock()
        self.firewall_api = fe.ComputeFirewallAPI(
            self.gce_service, dry_run=True)

        self.expected_rules = fe.FirewallRules(constants.TEST_PROJECT)
        self.current_rules = fe.FirewallRules(constants.TEST_PROJECT)

        self.project_sema = threading.BoundedSemaphore(value=1)
        self.operation_sema = threading.BoundedSemaphore(value=5)

        self.enforcer = fe.FirewallEnforcer(
            constants.TEST_PROJECT, self.firewall_api, self.expected_rules,
            self.current_rules, self.project_sema, self.operation_sema)
iap_rules_engine.py 文件源码 项目:forseti-security 作者: GoogleCloudPlatform 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, global_configs, rule_defs=None, snapshot_timestamp=None):
        """Initialization.

        Args:
            global_configs (dict): Global configurations.
            rule_defs (list): IAP rule definition dicts
            snapshot_timestamp (int): Snapshot timestamp.
        """
        super(IapRuleBook, self).__init__()
        self._rules_sema = threading.BoundedSemaphore(value=1)
        self.resource_rules_map = {}
        if not rule_defs:
            self.rule_defs = {}
        else:
            self.rule_defs = rule_defs
            self.add_rules(rule_defs)
        self.snapshot_timestamp = snapshot_timestamp
        self.org_res_rel_dao = org_resource_rel_dao.OrgResourceRelDao(
            global_configs)
        self.project_dao = project_dao.ProjectDao(global_configs)
test_threading.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_BoundedSemaphore_limit(self):
        # BoundedSemaphore should raise ValueError if released too often.
        for limit in range(1, 10):
            bs = threading.BoundedSemaphore(limit)
            threads = [threading.Thread(target=bs.acquire)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            threads = [threading.Thread(target=bs.release)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            self.assertRaises(ValueError, bs.release)
validateupdaterini.py 文件源码 项目:PortableApps.com-DevelopmentToolkit 作者: 3D1T0R 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def main_threaded(iniconfig):
    semaphore = BoundedSemaphore(CONCURRENCY_LIMIT)
    tasks = []
    for appid in iniconfig:
        section = iniconfig[appid]
        task = Thread(target=checker, args=(section, appid, semaphore))
        tasks.append(task)
        task.start()

    try:
        for t in tasks:
            t.join()
    except KeyboardInterrupt:
        for t in tasks:
            if hasattr(t, 'terminate'):  # multiprocessing
                t.terminate()
        print 'Validation aborted.'
        sys.exit(1)
test_threading.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_BoundedSemaphore_limit(self):
        # BoundedSemaphore should raise ValueError if released too often.
        for limit in range(1, 10):
            bs = threading.BoundedSemaphore(limit)
            threads = [threading.Thread(target=bs.acquire)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            threads = [threading.Thread(target=bs.release)
                       for _ in range(limit)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            self.assertRaises(ValueError, bs.release)
wfuzz.py 文件源码 项目:darkc0de-old-stuff 作者: tuwid 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def __init__(self,genreq,threads=20):
        self.genReq=genreq
        self.results=[]
        self.threads=threads
        self.run=True
        self.threads_list=[]
        self.nres=0
        self.mutex=1
        self.Semaphore_Mutex=threading.BoundedSemaphore(value=self.mutex)
sqPyfia.py 文件源码 项目:darkc0de-old-stuff 作者: tuwid 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self,req):
        self.req=req
        self.MD5Orig=""
        self.origWords=False

        self.dynamics=[]
        self.injResults=[]
        self.fingerResults=[]

        self.threads=1
        self.threaded=False
        self.semMUTEX=threading.BoundedSemaphore(value=1)
sqPyfia.py 文件源码 项目:darkc0de-old-stuff 作者: tuwid 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def setThreaded(self,THREADS):
        self.threaded=True
        self.nthreads=THREADS
        self.semTHREADS=threading.BoundedSemaphore(value=THREADS)
attacker.py 文件源码 项目:darkc0de-old-stuff 作者: tuwid 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__ (self):
        threading.Thread.__init__(self)
        Attacker.__Semaphore_Threads=threading.BoundedSemaphore(value=Attacker.__Threads)
attacker.py 文件源码 项目:darkc0de-old-stuff 作者: tuwid 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def setThreads(n):
        Attacker.__Threads=n
        Attacker.__Semaphore_Threads=threading.BoundedSemaphore(value=Attacker.__Threads)
thumbnail_limit_sema.py 文件源码 项目:Software-Architecture-with-Python 作者: PacktPublishing 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self, limit=10):
        self.limit = limit
        self.counter = threading.BoundedSemaphore(value=limit)
        self.count = 0
        # Start time
        self.start = time.time()
        # Image saving rate
        self.rate = 0
thumbnail_url_controller.py 文件源码 项目:Software-Architecture-with-Python 作者: PacktPublishing 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, limit=10):
        self.limit = limit
        self.counter = threading.BoundedSemaphore(value=limit)
        self.count = 0
        # Start time
        self.start = time.time()
        # Image saving rate
        self.rate = 0
sync.py 文件源码 项目:pathspider 作者: mami-project 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def acquire_n(self, value=1, blocking=True, timeout=None):
        """
        Acquire ``value`` number of tokens at once.

        The parameters ``blocking`` and ``timeout`` have the same semantics as
        :class:`BoundedSemaphore`.

        :returns: The same value as the last call to `BoundedSemaphore`'s
        :meth:`acquire` if :meth:`acquire` were called ``value`` times instead
        of the call to this method.
        """
        ret = None
        for _ in range(value):
            ret = self.acquire(blocking=blocking, timeout=timeout)
        return ret
sync.py 文件源码 项目:pathspider 作者: mami-project 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def release_n(self, value=1):
        """
        Release ``value`` number of tokens at once.

        :returns: The same value as the last call to `BoundedSemaphore`'s
        :meth:`release` if :meth:`release` were called ``value`` times instead
        of the call to this method.
        """
        ret = None
        for _ in range(value):
            ret = self.release()
        return ret
sublist3r.py 文件源码 项目:Belati 作者: aancw 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __init__(self, domain, subdomains=None, q=None, silent=False, verbose=True):
        subdomains = subdomains or []
        base_url = 'https://dnsdumpster.com/'
        self.live_subdomains = []
        self.engine_name = "DNSdumpster"
        self.threads = 70
        self.lock = threading.BoundedSemaphore(value=self.threads)
        self.q = q
        super(DNSdumpster, self).__init__(base_url, self.engine_name, domain, subdomains, q=q, silent=silent, verbose=verbose)
        return
sublist3r.py 文件源码 项目:Belati 作者: aancw 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(self, subdomains, ports):
        self.subdomains = subdomains
        self.ports = ports
        self.threads = 20
        self.lock = threading.BoundedSemaphore(value=self.threads)
test_threading.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_various_ops(self):
        # This takes about n/3 seconds to run (about n/3 clumps of tasks,
        # times about 1 second per clump).
        NUMTASKS = 10

        # no more than 3 of the 10 can run at once
        sema = threading.BoundedSemaphore(value=3)
        mutex = threading.RLock()
        numrunning = Counter()

        threads = []

        for i in range(NUMTASKS):
            t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
            threads.append(t)
            self.assertEqual(t.ident, None)
            self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t)))
            t.start()

        if verbose:
            print('waiting for all tasks to complete')
        for t in threads:
            t.join(NUMTASKS)
            self.assertTrue(not t.is_alive())
            self.assertNotEqual(t.ident, 0)
            self.assertFalse(t.ident is None)
            self.assertTrue(re.match('<TestThread\(.*, stopped -?\d+\)>',
                                     repr(t)))
        if verbose:
            print('all tasks done')
        self.assertEqual(numrunning.get(), 0)
test_contextlib.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def testWithBoundedSemaphore(self):
        lock = threading.BoundedSemaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked)
cracker.py 文件源码 项目:enteletaor 作者: cr0hn 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def cracking_threads(fn, port, config):
    global FOUND
    global THREADS

    th = []
    sem = threading.BoundedSemaphore(config.concurrency)

    with open(config.wordlist, "r") as f:
        for i, password in enumerate(f.readlines()):
            password = password.replace("\n", "")

            # log.debug("       -- Testing '%s'" % password)

            if FOUND is not None:
                break

            # Launch password
            t = threading.Thread(target=find_password_sem, args=(fn, sem, config.target, port, config.user, password, None, ))

            th.append(t)

            sem.acquire()
            t.start()

            if (i % 500) == 0:
                log.info("    >> %s passwords tested" % i)

    # Wait for ending
    for x in th:
        x.join()

    if FOUND is not None:
        log.error("  - Password found: %s" % FOUND)


# ----------------------------------------------------------------------
test_threading.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_various_ops(self):
        # This takes about n/3 seconds to run (about n/3 clumps of tasks,
        # times about 1 second per clump).
        NUMTASKS = 10

        # no more than 3 of the 10 can run at once
        sema = threading.BoundedSemaphore(value=3)
        mutex = threading.RLock()
        numrunning = Counter()

        threads = []

        for i in range(NUMTASKS):
            t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
            threads.append(t)
            self.assertIsNone(t.ident)
            self.assertRegexpMatches(repr(t), r'^<TestThread\(.*, initial\)>$')
            t.start()

        if verbose:
            print 'waiting for all tasks to complete'
        for t in threads:
            t.join(NUMTASKS)
            self.assertFalse(t.is_alive())
            self.assertNotEqual(t.ident, 0)
            self.assertIsNotNone(t.ident)
            self.assertRegexpMatches(repr(t), r'^<TestThread\(.*, \w+ -?\d+\)>$')
        if verbose:
            print 'all tasks done'
        self.assertEqual(numrunning.get(), 0)
test_contextlib.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def testWithBoundedSemaphore(self):
        lock = threading.BoundedSemaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked)

# This is needed to make the test actually run under regrtest.py!
test_threading.py 文件源码 项目:python2-tracer 作者: extremecoders-re 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_various_ops(self):
        # This takes about n/3 seconds to run (about n/3 clumps of tasks,
        # times about 1 second per clump).
        NUMTASKS = 10

        # no more than 3 of the 10 can run at once
        sema = threading.BoundedSemaphore(value=3)
        mutex = threading.RLock()
        numrunning = Counter()

        threads = []

        for i in range(NUMTASKS):
            t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
            threads.append(t)
            self.assertIsNone(t.ident)
            self.assertRegexpMatches(repr(t), r'^<TestThread\(.*, initial\)>$')
            t.start()

        if verbose:
            print 'waiting for all tasks to complete'
        for t in threads:
            t.join(NUMTASKS)
            self.assertFalse(t.is_alive())
            self.assertNotEqual(t.ident, 0)
            self.assertIsNotNone(t.ident)
            self.assertRegexpMatches(repr(t), r'^<TestThread\(.*, \w+ -?\d+\)>$')
        if verbose:
            print 'all tasks done'
        self.assertEqual(numrunning.get(), 0)
test_contextlib.py 文件源码 项目:python2-tracer 作者: extremecoders-re 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def testWithBoundedSemaphore(self):
        lock = threading.BoundedSemaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked)

# This is needed to make the test actually run under regrtest.py!


问题


面经


文章

微信
公众号

扫码关注公众号