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


Python threading.BoundedSemaphore类代码示例

本文整理汇总了Python中threading.BoundedSemaphore的典型用法代码示例。如果您正苦于以下问题:Python BoundedSemaphore类的具体用法?Python BoundedSemaphore怎么用?Python BoundedSemaphore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: __init__

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)
开发者ID:kosio,项目名称:EpiExplorer,代码行数:35,代码来源:DatasetProcessorManager.py

示例2: create_document

def create_document(main_url, max_connections=2, filepath=None):
    """Creates an EPUB document from a fanfic.

       main_url -- user given URL which should be the first chapter
       max_connections -- maximum number of simultaneous connections
           default: 2. This should be chosen with care as the Terms of Service
           of some of the websites states that you shouldn't cause more stress
           than a normal visitor.
       filepath -- optional path for the resulting Epub document
           By default filename is: %author - %title.epub in the current
           directory. %author and %title in the path are special, they're
           changed to the story author and title respectively."""
    global dl_semaphore
    dl_semaphore = BoundedSemaphore(max_connections)

    parse, parse_ch1 = get_parser(main_url)
    html_ch1, chapter_num, story_info = get_chapter1(main_url, parse_ch1)
    chapters = {}
    chapters[1] = html_ch1

    if story_info["cover_url"]:
        cover_image_req = Request(
            story_info["cover_url"], headers=story_info["cover_headers"])
        cover_image = urlopen(cover_image_req).read()
    else:
        cover_image = open("default.jpg", "rb").read()

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_connections+3) \
            as executor:
        parse_chapters = []
        download_urls = story_info["chapter_urls"]
        for ch in range(2, chapter_num+1):
            dl_semaphore.acquire()
            parse_chapters.append(
                executor.submit(get_chapter, download_urls[ch], ch, parse))
        for future in concurrent.futures.as_completed(parse_chapters):
            html, chapter_no = future.result()
            chapters[chapter_no] = html

    if not filepath:
        filepath = "{} - {}.epub".format(
            INVALID_CHARS.sub("-", story_info["author"]),
            INVALID_CHARS.sub("-", story_info["title"]))
    else:
        filepath = filepath.replace(
            "%author", INVALID_CHARS.sub("-", story_info["author"]))
        filepath = filepath.replace(
            "%title", INVALID_CHARS.sub("-", story_info["title"]))

    with zipfile.ZipFile(filepath, "w") as f:
        f.writestr("mimetype", MIMETYPE)
        f.writestr("META-INF/container.xml", CONTAINER_XML)
        f.writestr("Content/titlepage.html", TITLEPAGE_HTML)
        f.writestr("Content/styles.css", STYLES_CSS)
        f.writestr("Content/cover.jpg", cover_image)
        f.writestr("Content/toc.ncx", toc(story_info, chapter_num))
        f.writestr("Content/content.opf", contents(story_info, chapter_num))
        for ch in range(1, chapter_num+1):
            f.writestr("Content/Chapters/ch{}.html".format(ch), chapters[ch])
开发者ID:Morgus,项目名称:ffnet_epub,代码行数:59,代码来源:backend.py

示例3: execute_semaphored_threads

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
开发者ID:jsmolina,项目名称:asyncio_course,代码行数:14,代码来源:2ex_threads_fib.py

示例4: __init__

 def __init__(self, repos):
     self.ui = getglobalui()
     self.repos = repos
     self.config = repos.getconfig()
     self.tunnel = repos.getpreauthtunnel()
     self.usessl = repos.getssl()
     self.username = repos.getuser()
     self.password = None
     self.passworderror = None
     self.goodpassword = None
     self.hostname = repos.gethost()
     self.port = repos.getport()
     if self.port == None:
         self.port = 993 if self.usessl else 143
     self.sslclientcert = repos.getsslclientcert()
     self.sslclientkey = repos.getsslclientkey()
     self.sslcacertfile = repos.getsslcacertfile()
     if self.sslcacertfile is None:
         self.verifycert = None # disable cert verification
     self.delim = None
     self.root = None
     self.maxconnections = repos.getmaxconnections()
     self.availableconnections = []
     self.assignedconnections = []
     self.lastowner = {}
     self.semaphore = BoundedSemaphore(self.maxconnections)
     self.connectionlock = Lock()
     self.reference = repos.getreference()
     self.idlefolders = repos.getidlefolders()
     self.gss_step = self.GSS_STATE_STEP
     self.gss_vc = None
     self.gssapi = False
开发者ID:rayners,项目名称:offlineimap,代码行数:32,代码来源:imapserver.py

示例5: __init__

    def __init__(self):
	self.__logger = logging.getLogger("framework.facade")

	self.mCallBacks = None
	self.mutex = BoundedSemaphore(value=1)

	self.controller = None

	# Messages queue
	self.__qmsg = Queue()
	self.jman = None

	# results storage
	self.__results = Results()

	# load plugins
	self.__logger.info("* Loading plugins")
	self.load_plugins()

	# load session
	self.__logger.info("* Loading session")
	self.__cur_session = DataModel()
	if Settings().get(Settings.SEC_GRL, Settings.AUTO_SESSION) == 'True':
	    try:
		self.__cur_session = self.load_session()
	    except Exception:
		pass
开发者ID:xmendez,项目名称:misc,代码行数:27,代码来源:facade.py

示例6: __init__

 def __init__(self, storage_driver, table_info_repo, concurrent_tasks=1000,
              batch_chunk_size=25, schema_operation_timeout=300):
     self._storage_driver = storage_driver
     self._table_info_repo = table_info_repo
     self._batch_chunk_size = batch_chunk_size
     self._schema_operation_timeout = schema_operation_timeout
     self.__task_executor = ThreadPoolExecutor(concurrent_tasks)
     self.__task_semaphore = BoundedSemaphore(concurrent_tasks)
开发者ID:purpen,项目名称:magnetodb,代码行数:8,代码来源:simple_impl.py

示例7: VDOM_semaphore

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()
开发者ID:VDOMBoxGroup,项目名称:runtime2.0,代码行数:17,代码来源:semaphore.py

示例8: __init__

 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)
开发者ID:mason-fish,项目名称:holbertonschool-higher_level_programming,代码行数:9,代码来源:h_store.py

示例9: __init__

    def __init__(self, subscription):
      super(Next.Sink, self).__init__(subscription)
      self.gate = RLock()
      self.semaphore = BoundedSemaphore(1)
      self.semaphore.acquire()

      self.waiting = False
      self.kind = None
      self.value = None
      self.error = None
开发者ID:aguil,项目名称:RxPython,代码行数:10,代码来源:next.py

示例10: IDCardFactory

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
开发者ID:yokoi-h,项目名称:DesignPattern,代码行数:19,代码来源:IDCardFactory.py

示例11: MyThread

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()
开发者ID:deslum,项目名称:Twittya,代码行数:19,代码来源:Twittya.py

示例12: __init__

	def __init__(self, n_process=2, n_threads=5, n_taks=10, daemon=False):
		self.daemon = daemon
		self.n_taks = n_taks
		self.n_threads = n_threads
		self.n_process = n_process

		self.sem_threads = BoundedSemaphore(self.n_threads)
		self.sem_tasks = asyncio.BoundedSemaphore(self.n_taks)

		self.running_process = []
开发者ID:cr0hn,项目名称:TestingBench,代码行数:10,代码来源:process_threads_and_tasks_autoscale.py

示例13: __init__

    def __init__(self, subscription):
      super(Latest.Sink, self).__init__(subscription)
      self.gate = RLock()
      self.semaphore = BoundedSemaphore(1)
      self.semaphore.acquire()

      self.notificationAvailable = False
      self.kind = None
      self.value = None
      self.error = None
开发者ID:aguil,项目名称:RxPython,代码行数:10,代码来源:latest.py

示例14: __init__

 def __init__(self, dAmn, logging=True, debug=False):
     super(Logger, self).__init__()
     self.dAmn = dAmn
     self.logging = logging
     self.mute_channels = []
     self.debug = debug
     self.running = False
     self._run = False
     # Create locks
     self.qlock = BoundedSemaphore()
     #self.mlock = BoundedSemaphore()
     self.llock = BoundedSemaphore()
     # Create queues
     self.wqueue = [] # Output queue
     #self.mqueue = [] # Muter queue
     self.lqueue = [] # Logger queue. Just in case.
     # Just in case.
     self.subbing = False
     self.subthread = None
开发者ID:photofroggy,项目名称:Terra,代码行数:19,代码来源:console.py

示例15: __init__

    def __init__(self, repos):
        self.ui = getglobalui()
        self.repos = repos
        self.config = repos.getconfig()

        self.preauth_tunnel = repos.getpreauthtunnel()
        self.transport_tunnel = repos.gettransporttunnel()
        if self.preauth_tunnel and self.transport_tunnel:
            raise OfflineImapError('%s: ' % repos + \
              'you must enable precisely one '
              'type of tunnel (preauth or transport), '
              'not both', OfflineImapError.ERROR.REPO)
        self.tunnel = \
          self.preauth_tunnel if self.preauth_tunnel \
          else self.transport_tunnel

        self.username = \
          None if self.preauth_tunnel else repos.getuser()
        self.user_identity = repos.get_remote_identity()
        self.authmechs = repos.get_auth_mechanisms()
        self.password = None
        self.oauth2 = repos.getoauth2()
        self.oauth2url = repos.getoauth2url() if self.oauth2 else None
        self.oauth2clientid = repos.getoauth2clientid() if self.oauth2 else None
        self.oauth2clientsecret = repos.getoauth2clientsecret() if self.oauth2 else None
        self.oauth2refreshtoken = repos.getoauth2refreshtoken() if self.oauth2 else None
        self.passworderror = None
        self.goodpassword = None

        self.usessl = repos.getssl()
        self.hostname = \
          None if self.preauth_tunnel else repos.gethost()
        self.port = repos.getport()
        if self.port == None:
            self.port = 993 if self.usessl else 143
        self.sslclientcert = repos.getsslclientcert()
        self.sslclientkey = repos.getsslclientkey()
        self.sslcacertfile = repos.getsslcacertfile()
        self.sslversion = repos.getsslversion()
        if self.sslcacertfile is None:
            self.__verifycert = None # disable cert verification

        self.delim = None
        self.root = None
        self.maxconnections = repos.getmaxconnections()
        self.availableconnections = []
        self.assignedconnections = []
        self.lastowner = {}
        self.semaphore = BoundedSemaphore(self.maxconnections)
        self.connectionlock = Lock()
        self.reference = repos.getreference()
        self.idlefolders = repos.getidlefolders()
        self.gss_step = self.GSS_STATE_STEP
        self.gss_vc = None
        self.gssapi = False
开发者ID:javiergarmon,项目名称:offlineimap,代码行数:55,代码来源:imapserver.py


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