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


Python BoundedSemaphore.acquire方法代码示例

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


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

示例1: __init__

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
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,代码行数:37,代码来源:DatasetProcessorManager.py

示例2: create_document

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
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,代码行数:61,代码来源:backend.py

示例3: __init__

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class ThreadedGroup:
    """ An object to keep an arbitary number of workers on an easily-split
        task.
    """
    
    def __init__(self):
        """ Initialise self """
        
        self._job_count = 0 # The number of active jobs.
        self.lock = Lock() # Lock protecting job_count...
        self.semaphore = BoundedSemaphore(THREAD_COUNT)
        
    def start(self, func, *args, **kargs):
        """ Start the given job. This will block until there are free
            workers.
        """
        
        def job_wrapper():
            """ Run the job, then decrement the job count and release the
                semaphore.
            """
            try:
                func(*args, **kargs)
            finally:
                with self.lock:
                    self._job_count -= 1
                self.semaphore.release()
        
        # Create the job.
        job = Job(job_wrapper)
        # Acquire the semaphore and start.
        self.semaphore.acquire()
        with self.lock:
            self._job_count += 1
        job.start()
        
    def wait(self):
        """ Wait until all of the jobs are finished """
        
        print("Waiting for jobs to finish...")
        
        while self.get_job_count() != 0:
            # Block until another job ends.
            self.semaphore.acquire()
            
        print("Jobs finished!")

    def get_job_count(self):
        """ Return the current job count """

        with self.lock:
            return self._job_count
开发者ID:PlantandFoodResearch,项目名称:IrrigationAnimation,代码行数:54,代码来源:helpers.py

示例4: execute_semaphored_threads

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
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,代码行数:16,代码来源:2ex_threads_fib.py

示例5: IDCardFactory

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
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,代码行数:21,代码来源:IDCardFactory.py

示例6: MyThread

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
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,代码行数:21,代码来源:Twittya.py

示例7: rpiFIFOClass

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class rpiFIFOClass(deque):
	"""
	Implements the a Deque with BoundedSemaphore.
	Used as a FIFO buffer for the image file names (including the full path).
	Stores also the name of the current sub-folder.
	"""
	def __init__(self, *args):
		super(rpiFIFOClass,self).__init__(*args)
		self.FIFOSema  = BoundedSemaphore()
		self.crtSubDir = '/'
		self.camID     = ''
		
	def acquireSemaphore(self):
		self.FIFOSema.acquire()
		
	def releaseSemaphore(self):
		self.FIFOSema.release()		

	def __del__(self):
#		self.FIFOSema.release()	
		self.crtSubDir = ''
开发者ID:istvanzk,项目名称:rpicampy,代码行数:23,代码来源:rpififo.py

示例8: __init__

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class Results:
    def __init__(self):
	self.mutex = BoundedSemaphore(value=1)
	self.__issues = []

    def __len__(self):
	return len(self.__issues)

    # ------------------------------------------------
    # data functions
    # ------------------------------------------------
    def add_result(self, result):
	self.mutex.acquire()
	self.__issues.append(result)
	self.mutex.release()

    def get_result(self, index):
	self.mutex.acquire()
	item = self.__issues[index]
	self.mutex.release()

	return item
开发者ID:xmendez,项目名称:misc,代码行数:24,代码来源:proxies.py

示例9: semaphore

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class semaphore(object):
    """ Class encapsulating a semaphore to limit
    number of resources  """

    def __init__(self, *args):
        self.sem = BoundedSemaphore(args[0])
    
    def __call__(self, f):
        def semfunc(*args, **kwargs):
            try:
                print 'Trying to acquire sem=>',currentThread()
                self.sem.acquire()
                print 'Acquired sem=>',currentThread()
                try:
                    return f(*args, **kwargs)
                except Exception, e:
                    raise
            finally:
                self.sem.release()
                print 'Released sem=>',currentThread()

        
        return semfunc
开发者ID:bhramoss,项目名称:code,代码行数:25,代码来源:recipe-533135.py

示例10: BlockingRequest

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class BlockingRequest():
    
    def __init__(self):
        self.semaphore = BoundedSemaphore(1)
        self.exception = None
        self.response = None
        self.semaphore.acquire(True)
    
    def response_callback(self, response):
        self.response = response
        self.semaphore.release()
    
    
    def error_callback(self, exception):
        self.exception = exception
        self.semaphore.release()
    
    ''' returns the response or throws an exception '''
    def await_response(self):
        self.semaphore.acquire(True)        
        if self.exception :
            raise self.exception
        return self.response
开发者ID:sp00,项目名称:PyStrest,代码行数:25,代码来源:strestclient.py

示例11: BuildDispatcher

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class BuildDispatcher(Singleton):

    def _singleton_init(self):
        self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
        self.builders = dict()
        self.builders_lock = BoundedSemaphore()
        NotificationCenter().add_observer(self, 'handle_state_change', 'image.status')

    def handle_state_change(self, notification):
        status = notification.user_info['new_status']
        if(status in ('COMPLETED', 'FAILED', 'DELETED', 'DELETEFAILED')):
            self.builders_lock.acquire()
            image_id = notification.sender.identifier
            if(image_id in self.builders):
                del self.builders[image_id]
                self.log.debug('Removed builder from BuildDispatcher on notification from image %s: %s' % (image_id, status))
            self.builders_lock.release()

    def builder_for_base_image(self, template, parameters=None):
        builder = Builder()
        builder.build_image_from_template(template, parameters=parameters)
        self.builders_lock.acquire()
        try:
            self.builders[builder.base_image.identifier] = builder
        finally:
            self.builders_lock.release()
        return builder

    def builder_for_target_image(self, target, image_id=None, template=None, parameters=None):
        builder = Builder()
        builder.customize_image_for_target(target, image_id, template, parameters)
        self.builders_lock.acquire()
        try:
            self.builders[builder.target_image.identifier] = builder
        finally:
            self.builders_lock.release()
        return builder

    def builder_for_provider_image(self, provider, credentials, target, image_id=None, template=None, parameters=None):
        builder = Builder()
        builder.create_image_on_provider(provider, credentials, target, image_id, template, parameters)
        self.builders_lock.acquire()
        try:
            self.builders[builder.provider_image.identifier] = builder
        finally:
            self.builders_lock.release()
        return builder
开发者ID:jguiditta,项目名称:imagefactory,代码行数:49,代码来源:BuildDispatcher.py

示例12: Store

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class Store(object):
    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)

    def enter(self):
        self.sema.acquire()

    def buy(self):
        sleep(randint(5, 10))
        if self.item_number:
            purchase = True
            self.item_number -= 1
        else:
            purchase = False

        self.sema.release()
        return purchase
开发者ID:mason-fish,项目名称:holbertonschool-higher_level_programming,代码行数:26,代码来源:h_store.py

示例13: map_task

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
def map_task(source, func, thread_limit=10):
  '''Run func in up to thread_limit threads, with data from
  source arg passed into it.

  The arg source must be iterable. map_task() will call next()
  each time a free thread is available.

  The function will block until all of the tasks are completed.
  '''
  assert thread_limit > 0
  e = BoundedSemaphore(thread_limit)
  task_list = []
  for i in xrange(0, thread_limit):
    task_list.append(task(func, e))
  iterer = source.__iter__()
  data = None
  while 1:
    try:
      if data is None:
        data = iterer.next()
      t = get_free_task(task_list)
      if t:
        t.data = data
        t.start()
        data = None
      else:
#        print >> sys.stderr, 'waiting for e'
        e.acquire()
        e.release()
    except StopIteration:
    # iteration is stopped
#      print >>sys.stderr, 'terminating'
      for a_task in task_list:
#        print >>sys.stderr, 'terminating ' + str(a_task)
        a_task.terminate_and_join()
      return
开发者ID:simplosophy,项目名称:webcache,代码行数:38,代码来源:task_runner.py

示例14: VDOM_semaphore

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
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,代码行数:19,代码来源:semaphore.py

示例15: IMAPServer

# 需要导入模块: from threading import BoundedSemaphore [as 别名]
# 或者: from threading.BoundedSemaphore import acquire [as 别名]
class IMAPServer(object):
    """Initializes all variables from an IMAPRepository() instance

    Various functions, such as acquireconnection() return an IMAP4
    object on which we can operate.

    Public instance variables are: self.:
     delim The server's folder delimiter. Only valid after acquireconnection()
    """

    def __init__(self, repos):
        """:repos: a IMAPRepository instance."""

        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.passworderror = None
        self.goodpassword = None

        self.usessl = repos.getssl()
        self.useipv6 = repos.getipv6()
        if self.useipv6 is True:
            self.af = socket.AF_INET6
        elif self.useipv6 is False:
            self.af = socket.AF_INET
        else:
            self.af = socket.AF_UNSPEC
        self.hostname = None if self.transport_tunnel or self.preauth_tunnel else repos.gethost()
        self.port = repos.getport()
        if self.port is 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.
                                     # This way of working sucks hard...
        self.fingerprint = repos.get_ssl_fingerprint()
        self.tlslevel = repos.gettlslevel()
        self.sslversion = repos.getsslversion()
        self.starttls = repos.getstarttls()

        if self.usessl \
           and self.tlslevel is not "tls_compat" \
           and self.sslversion is None:
            raise Exception("When 'tls_level' is not 'tls_compat' "
                "the 'ssl_version' must be set explicitly.")

        self.oauth2_refresh_token = repos.getoauth2_refresh_token()
        self.oauth2_access_token = repos.getoauth2_access_token()
        self.oauth2_client_id = repos.getoauth2_client_id()
        self.oauth2_client_secret = repos.getoauth2_client_secret()
        self.oauth2_request_url = repos.getoauth2_request_url()
        self.oauth2_access_token_expires_at = None

        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_vc = None
        self.gssapi = False

        # In order to support proxy connection, we have to override the
        # default socket instance with our own socksified socket instance.
        # We add this option to bypass the GFW in China.
        self.proxied_socket = self._get_proxy('proxy', socket.socket)

        # Turns out that the GFW in China is no longer blocking imap.gmail.com
        # However accounts.google.com (for oauth2) definitey is.  Therefore
        # it is not strictly necessary to use a proxy for *both* IMAP *and*
        # oauth2, so a new option is added: authproxy.

        # Set proxy for use in authentication (only) if desired.
        # If not set, is same as proxy option (compatible with current configs)
        # To use a proxied_socket but not an authproxied_socket
        # set authproxy = '' in config
        self.authproxied_socket = self._get_proxy('authproxy',
#.........这里部分代码省略.........
开发者ID:OfflineIMAP,项目名称:offlineimap,代码行数:103,代码来源:imapserver.py


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