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


Python Semaphore.acquire方法代码示例

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


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

示例1: setCurrentSimulationTime

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
 def setCurrentSimulationTime(self, currentSimulationTime):
    
     semaphore = Semaphore()
     semaphore.acquire()
     self.__currentSimulationTime = currentSimulationTime
     semaphore.release()
     return self.__currentSimulationTime
开发者ID:krohan100,项目名称:pydssim,代码行数:9,代码来源:abstract_simulation.py

示例2: Msg

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
class Msg(object):
    """
    Data structure encapsulating a message.
    """

    def __init__(self, size):
        self.s_e = Semaphore(1)
        self.s_f = Semaphore(0)
        self.s_buf = Array(ct.c_ubyte, size)

    def send(self, func):
        self.s_e.acquire()
        self.s_buf.acquire()
        send_result = func(self.s_buf._obj)
        self.s_buf.release()
        self.s_f.release()
        return send_result

    def recv(self, func):
        self.s_f.acquire()
        self.s_buf.acquire()
        recv_result = func(self.s_buf._obj)
        self.s_buf.release()
        self.s_e.release()
        return recv_result
开发者ID:huhoo,项目名称:neon,代码行数:27,代码来源:image.py

示例3: block_until_processed

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
def block_until_processed(cookie_jar: CookieJar, cookie_paths: Sequence[str],
                          expected_number_of_calls_to_mark_as_complete: int):
    """
    Puts the given cookies into the cookie jar and wait until they have been completed/marked for reprocessing.
    :param cookie_jar: the cookie jar to put cookies to process into
    :param cookie_paths: the cookie paths to process
    :param expected_number_of_calls_to_mark_as_complete: the number of calls expected to the Cookie jar's
    `mark_as_complete` method
    """
    if cookie_jar.queue_length() != 0:
        raise RuntimeError("Already cookies in the jar")

    mark_as_complete_semaphore = Semaphore(0)
    original_mark_as_complete = cookie_jar.mark_as_complete

    def mark_as_complete(path: str):
        mark_as_complete_semaphore.release()
        original_mark_as_complete(path)

    cookie_jar.mark_as_complete = MagicMock(side_effect=mark_as_complete)

    for cookie_path in cookie_paths:
        cookie_jar.mark_for_processing(cookie_path)

    calls_to_mark_as_complete = 0
    while calls_to_mark_as_complete != expected_number_of_calls_to_mark_as_complete:
        mark_as_complete_semaphore.acquire()
        assert cookie_jar.mark_as_complete.call_count <= expected_number_of_calls_to_mark_as_complete
        calls_to_mark_as_complete += 1

    assert calls_to_mark_as_complete == cookie_jar.mark_as_complete.call_count
开发者ID:wtsi-hgi,项目名称:cookie-monster,代码行数:33,代码来源:_helpers.py

示例4: getNeighbors

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
 def getNeighbors(self, peer):
             
     semaphore = Semaphore()
     semaphore.acquire()
     neighbors = self.__layout[peer.getId()].getNeighbors()
     semaphore.release()
     return neighbors
开发者ID:krohan100,项目名称:pydssim,代码行数:9,代码来源:topology.py

示例5: launch_workers

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
def launch_workers(outfile, start_index, end_index, score_flag, force, verbose):
	BASE_URL = "http://www.ign.com/games/all-ajax?startIndex="
	
	
	# Synchronization Tools
	num_workers = Semaphore(MAX_NUM_PROCESSES)
	outfile_lock = Lock()
	urlopen_lock = Lock()
	stderr_lock = Lock()
	print_lock = Lock()
	
	# Write the categories
	if (outfile != None):
		outfile.write("title,link,platform,publisher,score,date\n")

	# Launch the workers
	processes = []
	curr_index = start_index;
	while curr_index <= end_index:
		curr_url = BASE_URL + str(curr_index)
	 	worker = Process(target=open_url_and_parse,
	 		args=(outfile, curr_url, score_flag, force, verbose,
	 			outfile_lock, urlopen_lock, stderr_lock, print_lock,
	 			num_workers))
	 	processes.append(worker)
	 	if verbose:
			print_lock.acquire()
			print "Launching worker for url: %s" % curr_url
			print_lock.release()
	 	num_workers.acquire()
	 	worker.start()
	 	curr_index += INDEX_INCREMENT; 
	for p in processes:
	 	p.join()
开发者ID:akshayka,项目名称:video-games,代码行数:36,代码来源:IGN_scraper.py

示例6: Msg

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
class Msg(object):
    """
    TODO: Not documenting this class because it may go away.
    """

    def __init__(self, size):
        self.s_e = Semaphore(1)
        self.s_f = Semaphore(0)
        self.s_buf = Array(ct.c_ubyte, size)

    def send(self, func):
        self.s_e.acquire()
        self.s_buf.acquire()
        send_result = func(self.s_buf._obj)
        self.s_buf.release()
        self.s_f.release()
        return send_result

    def recv(self, func):
        self.s_f.acquire()
        self.s_buf.acquire()
        recv_result = func(self.s_buf._obj)
        self.s_buf.release()
        self.s_e.release()
        return recv_result
开发者ID:rupertsmall,项目名称:neon,代码行数:27,代码来源:image.py

示例7: run

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
def run():
    algo = parameters["algo"]
    files = [open(x) for x in parameters["files"]]
    configs = []
    p = parameters["params"]
    max_processes = 3
    semaphore = Semaphore(max_processes)

    # generate configurations as compination of possible
    # keys and product of values
    for keys in it.combinations(p.keys(), len(p.keys())):
        v = [p[k] for k in keys]
        for values in it.product(*v):
            config = {}
            for i, k in enumerate(keys):
                config[k] = values[i]
            configs.append(config)
    for f in files:
        for conf in configs:
            config = {"FILENAME": f.name}
            config.update(conf)

            f.seek(0)
            num_vars, clauses = parser.parse(f)

            p = MyProcess(target=run_algorithm, args=(algo, num_vars, clauses, config, semaphore))

            semaphore.acquire()
            p.start()
开发者ID:domoritz,项目名称:SoSAT,代码行数:31,代码来源:benchmark.py

示例8: getPeerID

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
 def getPeerID(self, peerId):
     
     semaphore = Semaphore()
     semaphore.acquire()
     peer = self.__layout[peerId]
     semaphore.release()
     return peer
开发者ID:krohan100,项目名称:pydssim,代码行数:9,代码来源:abstract_network.py

示例9: run

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
    def run(self, tasks, build_config, parallel_threads):
        semaphore = Semaphore(parallel_threads)
        process_finished_notify = Condition(Lock())
        while tasks.count_buildable_tasks() > 0:
            task = tasks.get_next()

            if task is None:
                self.wait_tasks_to_complete(parallel_threads, process_finished_notify, semaphore)
                continue

            semaphore.acquire()
            task.state = Task.State.RUNNING
            logging.debug("Starting task %s", task.name)
            self.start_new_process(process_finished_notify, semaphore, self.process_job, task, build_config)

        self.wait_tasks_to_complete(parallel_threads, process_finished_notify, semaphore)

        if tasks.count(Task.State.FAILED) > 0:
            logging.error('Some packages failed to build.')
            logging.error("  %s", tasks.print_name(Task.State.FAILED))
            return 1
        if tasks.count(Task.State.RUNNING) > 0:
            logging.error('Something went wrong, there are still some running tasks.')
            return 1
        if tasks.count(Task.State.NEW) > 0:
            logging.error('Something went wrong, there are still unprocessed tasks.')
            return 1

        logging.info("Build completed successfully.")
        return 0
开发者ID:project-ncl,项目名称:pnc-cli,代码行数:32,代码来源:tasks.py

示例10: countNeighbors

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
 def countNeighbors(self, peer):
                    
     semaphore = Semaphore()
     semaphore.acquire()
     
     count = peer.countNeighbors()
     semaphore.release()
     return count
开发者ID:krohan100,项目名称:pydssim,代码行数:10,代码来源:topology.py

示例11: getNeighborIt

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
 def getNeighborIt(self, peer):
             
     semaphore = Semaphore()
     semaphore.acquire()
     neighbors = []
     for neighbor in self.__layout[peer.getId()].getNeighbors():
         neighbors.append(neighbor.getTargetPeer())
     neighborIt = neighbors.__iter__()
     semaphore.release()
     return neighborIt
开发者ID:krohan100,项目名称:pydssim,代码行数:12,代码来源:topology.py

示例12: addPeer

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
 def addPeer(self, peer):
     
     if self.__layout.has_key(peer.getPID()):
         return False
     
     semaphore = Semaphore()
     semaphore.acquire()
     
     self.__layout[peer.getPID()] = peer
     semaphore.release()
     NetworkLogger().resgiterLoggingInfo("Add peer %s in Layout Network "%(peer.getPID()))
     return self.__layout.has_key(peer.getPID())
开发者ID:krohan100,项目名称:pydssim,代码行数:14,代码来源:abstract_network.py

示例13: __init__

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
class Barrier:
    def __init__(self, n):
        self.n = n
        self.counter = SharedCounter(0)
        self.barrier = Semaphore(0)

    def wait(self):
        with self.counter.lock:
            self.counter.val.value += 1
            if self.counter.val.value == self.n: 
                self.barrier.release()
        self.barrier.acquire()
        self.barrier.release()
开发者ID:traai,项目名称:async-deep-rl,代码行数:15,代码来源:shared_utils.py

示例14: WorkQueue

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
class WorkQueue(object):
    def __init__(self):
        self.request_rfd, self.request_wfd = os.pipe()
        self.response_rfd, self.response_wfd = os.pipe()
        self.response_reader = ResponseReader(self.response_rfd)
        self.request_sem = Semaphore()
        self.response_sem = Semaphore()

    def submit_request(self, id, address, head, body):
        try:
            ip_str, port = address
            ipa, ipb, ipc, ipd = map(int, ip_str.split("."))
        except:
            port = ipa = ipb = ipc = ipd = 0
        os.write(
            self.request_wfd, REQUEST_HEADER.pack(id, ipa, ipb, ipc, ipd, port, len(head), len(body)) + head + body
        )

    def get_request(self):
        self.request_sem.acquire()
        header = ""
        bytes_to_read = REQUEST_HEADER.size
        while bytes_to_read:
            header += os.read(self.request_rfd, bytes_to_read)
            bytes_to_read = REQUEST_HEADER.size - len(header)
        id, ipa, ipb, ipc, ipd, port, head_len, body_len = REQUEST_HEADER.unpack(header)

        head = StringIO()
        bytes_to_read = head_len
        while bytes_to_read:
            head.write(os.read(self.request_rfd, bytes_to_read))
            bytes_to_read = head_len - head.tell()

        body = StringIO()
        bytes_to_read = body_len
        while bytes_to_read:
            body.write(os.read(self.request_rfd, bytes_to_read))
            bytes_to_read = body_len - body.tell()

        self.request_sem.release()
        return id, (".".join(map(str, [ipa, ipb, ipc, ipd])), port), head.getvalue(), body.getvalue()

    def submit_response(self, id, response):
        self.response_sem.acquire()
        response_output = response.output()
        keep_alive = "\x01" if response.headers.get("Connection") == "Keep-Alive" else "\x00"
        os.write(self.response_wfd, RESPONSE_HEADER.pack(id, len(response_output)) + response_output + keep_alive)
        self.response_sem.release()

    def get_response(self):
        return self.response_reader.read()
开发者ID:gattis,项目名称:magnum-py,代码行数:53,代码来源:ipc.py

示例15: ForkingWorker

# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import acquire [as 别名]
class ForkingWorker(BaseWorker):

    def __init__(self, num_processes=1):
        # Set up sync primitives, to communicate with the spawned children
        self._semaphore = Semaphore(num_processes)
        self._slots = Array('i', [0] * num_processes)

    def spawn_child(self):
        """Forks and executes the job."""
        self._semaphore.acquire()    # responsible for the blocking

        # Select an empty slot from self._slots (the first 0 value is picked)
        # The implementation guarantees there will always be at least one empty slot
        for slot, value in enumerate(self._slots):
            if value == 0:
                break

        # The usual hardcore forking action
        child_pid = os.fork()
        if child_pid == 0:
            # Within child

            # Disable signal handlers
            signal.signal(signal.SIGINT, signal.SIG_IGN)
            signal.signal(signal.SIGTERM, signal.SIG_IGN)

            random.seed()
            try:
                self.fake_work()
            finally:
                # This is the new stuff.  Remember, we're in the child process
                # currently. When all work is done here, free up the current
                # slot (by writing a 0 in the slot position).  This
                # communicates to the parent that the current child has died
                # (so can safely be forgotten about).
                self._slots[slot] = 0
                self._semaphore.release()
                os._exit(0)
        else:
            # Within parent, keep track of the new child by writing its PID
            # into the first free slot index.
            self._slots[slot] = child_pid

    def wait_for_children(self):
        for child_pid in self._slots:
            if child_pid != 0:
                os.waitpid(child_pid, 0)

    def get_id(self):
        return os.getpid()
开发者ID:nvie,项目名称:worker-experiment,代码行数:52,代码来源:forking.py


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