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


Python Event.is_set方法代码示例

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


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

示例1: single_output

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
def single_output(stop_event: Event):
    print("single output get queue:")
    sum_limit = 1000
    counter = 0
    manager, output_q = get_queue_client(QueueManager.MachineSettingCrawler, QueueManager.Method_Whois_Input)
    while not stop_event.is_set():
        try:
            while not output_q.empty() or not stop_event.is_set():
                result = output_q.get(False, 1)
                counter += 1
                if isinstance(result, list):
                    for item in result:
                        print("server queue output:", str(item), "count:", counter)
                else:
                    # print(result)
                    pass
                if counter/sum_limit > 0 and counter % sum_limit==0:
                    print("current output count is:", counter)
                time.sleep(0.000001)
        except Exception as ex:
            pass
            # manager, output_q = get_queue_client(QueueManager.MachineSettingCrawler, QueueManager.Method_Whois_Output)
        finally:
            print("going to sleep.")
            time.sleep(1)
开发者ID:paulnaoki,项目名称:DomainFinderSrcUniversal,代码行数:27,代码来源:SiteCheckerTest.py

示例2: __init__

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class Transcoder:
    def __init__(self):
        self.stopping = Event()

    def stop(self):
        logger.debug("Preventing new transcoding processes.")
        self.stopping.set()

    def transcode(self, path, format='mp3', bitrate=False):
        if self.stopping.is_set():
            return
        try:
            stop = Event()
            start_time = time.time()
            parent_conn, child_conn = Pipe()
            process = Process(target=transcode_process,
                    args=(child_conn, path, stop, format, bitrate))
            process.start()
            while not (self.stopping.is_set() or stop.is_set()):
                data = parent_conn.recv()
                if not data:
                    break
                yield data
            logger.debug("Transcoded %s in %0.2f seconds." % (path.encode(cfg['ENCODING']), time.time() - start_time))
        except GeneratorExit:
            stop.set()
            logger.debug("User canceled the request during transcoding.")
        except:
            stop.set()
            logger.warn("Some type of error occured during transcoding.")
        finally:
            parent_conn.close()
            process.join()
开发者ID:daveisadork,项目名称:Blofeld,代码行数:35,代码来源:transcode.py

示例3: Worker

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class Worker(Process):
    def __init__(self, buffer, reorder_buffer, job):
        Process.__init__(self)
        self.buffer = buffer
        self.reorder_buffer = reorder_buffer
        self.job = job
        self.event = Event()

    def run(self):
        self.event.set()
        while self.event.is_set():
            try:
                block_number, data = self.buffer.get()
            except IOError, e:
                if e.errno == errno.EINTR:
                    data = EOF

            if data == EOF:
                self.stop()
                break
            worked_data = self.job(data)

            while self.event.is_set():
                try:
                    self.reorder_buffer.put(block_number, worked_data)
                    break
                except IndexError:
                    # Block num bigger than expected,
                    # wait untill ReorderBuffer start
                    # processing blocks in this range
                    time.sleep(0.1)
                except IOError, e:
                    if e.errno == errno.EINTR:
                        self.stop()
开发者ID:igorbonadio,项目名称:carbono,代码行数:36,代码来源:work_manager.py

示例4: Cluster

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class Cluster(object):
    def __init__(self, broker=None):
        self.broker = broker or get_broker()
        self.sentinel = None
        self.stop_event = None
        self.start_event = None
        self.pid = current_process().pid
        self.host = socket.gethostname()
        self.timeout = Conf.TIMEOUT
        signal.signal(signal.SIGTERM, self.sig_handler)
        signal.signal(signal.SIGINT, self.sig_handler)

    def start(self):
        # Start Sentinel
        self.stop_event = Event()
        self.start_event = Event()
        self.sentinel = Process(target=Sentinel, args=(self.stop_event, self.start_event, self.broker, self.timeout))
        self.sentinel.start()
        logger.info(_("Q Cluster-{} starting.").format(self.pid))
        while not self.start_event.is_set():
            sleep(0.1)
        return self.pid

    def stop(self):
        if not self.sentinel.is_alive():
            return False
        logger.info(_("Q Cluster-{} stopping.").format(self.pid))
        self.stop_event.set()
        self.sentinel.join()
        logger.info(_("Q Cluster-{} has stopped.").format(self.pid))
        self.start_event = None
        self.stop_event = None
        return True

    def sig_handler(self, signum, frame):
        logger.debug(_("{} got signal {}").format(current_process().name, Conf.SIGNAL_NAMES.get(signum, "UNKNOWN")))
        self.stop()

    @property
    def stat(self):
        if self.sentinel:
            return Stat.get(self.pid)
        return Status(self.pid)

    @property
    def is_starting(self):
        return self.stop_event and self.start_event and not self.start_event.is_set()

    @property
    def is_running(self):
        return self.stop_event and self.start_event and self.start_event.is_set()

    @property
    def is_stopping(self):
        return self.stop_event and self.start_event and self.start_event.is_set() and self.stop_event.is_set()

    @property
    def has_stopped(self):
        return self.start_event is None and self.stop_event is None and self.sentinel
开发者ID:aparsons,项目名称:django-q,代码行数:61,代码来源:cluster.py

示例5: CheckerProcess

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class CheckerProcess(Process):
    def __init__(self, team_id, checks, db_host, db_port, db_name, check_delay):
        super(CheckerProcess, self).__init__()
        self.team_id = team_id
        self.db = MongoDBWrapper(db_host, int(db_port), db_name)
        self.check_delay = int(check_delay)
        self.shutdown_event = Event()
        self.checks = []
        for check in checks:
            check_db_data = self.db.get_specific_check(check.check_id, check.check_class.check_type)
            if len(check_db_data) > 0:
                check_data = check_db_data[0]
                if issubclass(check.check_class, InjectCheck):
                    check_obj = check.check_class(check_data['machine'], team_id, db_host, db_port, db_name, check_data['time_to_check'])
                    self.checks.append(Checker(check.check_id, check_obj, check_obj.time_to_run))
                elif issubclass(check.check_class, (ServiceCheck, AttackerCheck)):
                    check_obj = check.check_class(check_data['machine'], team_id, db_host, db_port, db_name)
                    self.checks.append(Checker(check.check_id, check_obj, datetime.now()))
        random.shuffle(self.checks, random.random)

    def run(self):
        while not self.shutdown_event.is_set():
            self.run_checks()

    def run_checks(self):
        indices_to_remove = []
        for i in range(0, len(self.checks)):
            check = self.checks[i]
            check_obj = copy(check.object)
            now = datetime.now()
            print check.time_to_run, '<', now, '=', check.time_to_run < now
            if check.time_to_run < now:
                check_process = Process(target=check_obj.run_check)
                check_process.start()
                check_process.join(check_obj.timeout)
                if check_process.is_alive():
                    check_process.terminate()
                score = check_obj.score
                if issubclass(type(check_obj), InjectCheck):
                    self.db.complete_inject_check(check.id, self.team_id, datetime.now(), score)
                    #self.checks[:] = [obj for obj in self.checks if not obj == check]
                    indices_to_remove.append(i)
                elif issubclass(type(check_obj), ServiceCheck):
                    self.db.complete_service_check(check.id, self.team_id, datetime.now(), score)
                    check.timestamp = datetime.now() + timedelta(seconds=self.check_delay)
                elif issubclass(type(check_obj), AttackerCheck):
                    self.db.complete_attacker_check(check.id, self.team_id, datetime.now(), score)
                    check.timestamp = datetime.now() + timedelta(seconds=self.check_delay)
                self.db.calculate_scores_for_team(self.team_id)
            if self.shutdown_event.is_set():
                break
        indices_to_remove.sort(reverse=True)
        for i in indices_to_remove:
            del self.checks[i]
开发者ID:smartboyathome,项目名称:Wonderland-Engine,代码行数:56,代码来源:checker_process.py

示例6: __init__

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class SharedEvent:
    def __init__(self, timeout = 0.7):
        self._rest = Event()
        self._exit = Event()
        self.timeout = timeout
        self._rest.set()

    def shutdown(self):
        while self._rest.is_set():
            time.sleep(self.timeout)
        return self._exit.is_set()
开发者ID:zengxunxun,项目名称:filestat,代码行数:13,代码来源:process.py

示例7: ParserThread

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class ParserThread(Process):
    """ Class writes simulated data to navigation system and
    receives results """

    def __init__(self, device, baudrate, q):
        Process.__init__(self)
        self.device = device
        self.baudrate = baudrate
        self.q = q
        self.__stop = Event()

    def stop(self):
        self.__stop.set()

    def mav_dispatch(self, m):
        try:
            self.q.put_nowait(m)
        except Full:
            pass

    def run(self):
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        print("*** parser process started. Pid:", os.getpid())
        master = mavutil.mavlink_connection(self.device, self.baudrate, dialect="lapwing")

        while not self.__stop.is_set():
            m = master.recv_msg()
            if (m is not None):
                dbg_print(m)
                self.mav_dispatch(m)

        master.close()
        print("*** parser process stopped")
开发者ID:Smolyarov,项目名称:u2,代码行数:35,代码来源:parserthread.py

示例8: AMQPInput

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class AMQPInput(BaseInput):

	log = logging.getLogger("%s.AMQPInput" %(__name__))

	def __init__(self, aggr_config, namespace="local", config={}):
		super(AMQPInput, self).__init__(aggr_config, namespace, config)
		self.exit = Event()

	def run(self):
		self.log.info("Connecting to (%s): %s" %(self.outputType, self.outputUri))
		aggrConn = self.connectAggregator()

		rabbitmqConsumer = RabbitMQConsumer(**self.config)

		def callback(event):
			if not event.has_key('namespace'):
				event['namespace'] = self.namespace

			aggrConn.send(event)

			if self.exit.is_set():
				aggrConn.close()
				rabbitmqConsumer.stop()

		rabbitmqConsumer.userCallbacks = [ callback ]	
		rabbitmqConsumer.start()

	def shutdown(self):
		self.exit.set()
开发者ID:euforia,项目名称:EventReactor,代码行数:31,代码来源:__init__.py

示例9: StoppableProcess

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class StoppableProcess(Process):
    """ Base class for Processes which require the ability
    to be stopped by a process-safe method call
    """

    def __init__(self):
        self._should_stop = Event()
        self._should_stop.clear()
        super(StoppableProcess, self).__init__()

    def join(self, timeout=0):
        """ Joins the current process and forces it to stop after
        the timeout if necessary

        :param timeout: Timeout duration in seconds
        """
        self._should_stop.wait(timeout)
        if not self.should_stop():
            self.stop()
        super(StoppableProcess, self).join(0)

    def stop(self):
        self._should_stop.set()

    def should_stop(self):
        return self._should_stop.is_set()

    def __repr__(self):
        return "<%s(should_stop=%s)>" % (
            self.__class__.__name__, self.should_stop())
开发者ID:ddale,项目名称:pymeasure,代码行数:32,代码来源:process.py

示例10: test_recycle

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
def test_recycle(broker, monkeypatch):
    # set up the Sentinel
    broker.list_key = 'test_recycle_test:q'
    async('django_q.tests.tasks.multiply', 2, 2, broker=broker)
    async('django_q.tests.tasks.multiply', 2, 2, broker=broker)
    async('django_q.tests.tasks.multiply', 2, 2, broker=broker)
    start_event = Event()
    stop_event = Event()
    # override settings
    monkeypatch.setattr(Conf, 'RECYCLE', 2)
    monkeypatch.setattr(Conf, 'WORKERS', 1)
    # set a timer to stop the Sentinel
    threading.Timer(3, stop_event.set).start()
    s = Sentinel(stop_event, start_event, broker=broker)
    assert start_event.is_set()
    assert s.status() == Conf.STOPPED
    assert s.reincarnations == 1
    async('django_q.tests.tasks.multiply', 2, 2, broker=broker)
    async('django_q.tests.tasks.multiply', 2, 2, broker=broker)
    task_queue = Queue()
    result_queue = Queue()
    # push two tasks
    pusher(task_queue, stop_event, broker=broker)
    pusher(task_queue, stop_event, broker=broker)
    # worker should exit on recycle
    worker(task_queue, result_queue, Value('f', -1))
    # check if the work has been done
    assert result_queue.qsize() == 2
    # save_limit test
    monkeypatch.setattr(Conf, 'SAVE_LIMIT', 1)
    result_queue.put('STOP')
    # run monitor
    monitor(result_queue)
    assert Success.objects.count() == Conf.SAVE_LIMIT
    broker.delete_queue()
开发者ID:fle-internal,项目名称:django-q,代码行数:37,代码来源:test_cluster.py

示例11: do_the_transfer

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
def do_the_transfer(source, target, cmd, parameters, filename, size, runname, clean=False, logfile=None):

    logger.debug("starting transfer")

    if ("gsiftp" in source) or ("gsiftp" in target):
        if not cmd:
            cmd = "globus-url-copy"

        transfer = Gridftp(source+filename, target+filename, cmd, parameters)
    else:
        if not cmd:
            cmd = 'scp'
        transfer = Scp(source+filename, target+filename, cmd, parameters)

    results_sizes[runname] = size
    results_transfer[runname] = transfer

    file_temp = get_source_file(size, working_directory, clean)

    transfer.prepare(file_temp, clean)

    #logger.info("Starting transfer:\n\t"+str(gridftp))

    transfer_cmd = transfer.transfer_command()

    event = Event()
    e = threading.Thread(target=execution, args=(event, transfer))
    t = threading.Thread(target=timing, args=(event, size, results_time, results_speed, runname, transfer, logfile))
    t.start()
    e.start()
    while not event.is_set():
        time.sleep(1)
开发者ID:makkus,项目名称:scripts-work,代码行数:34,代码来源:transfer.py

示例12: CaptureProcess

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class CaptureProcess(Process):
    """A process that fills a queue with images as captured from 
    a camera feed"""
    def __init__(self, capture, imageQueue):
        Process.__init__(self, name="Capture")
        self.imageQueue = imageQueue
        self.capture = capture
        self.keepGoing = Event()
        self.keepGoing.set()
        self.daemon = True

    def run(self):
        print "CaptureProcess pid: %s" % (self.pid,)
        while self.keepGoing.is_set():
            image = captureImage(self.capture)
#            sys.stdout.write(".")
            try:
                self.imageQueue.put(serializeImage(image), block=True, timeout=0.25)
            except FullException:
                try:
                    _ = self.imageQueue.get_nowait()
                except:
                    pass  # Try to clear the queue, but don't worry if someone snatches it first
    def stop(self):
        self.keepGoing.clear()
开发者ID:jbrowne,项目名称:UCSBsketch,代码行数:27,代码来源:ContinuousCapture.py

示例13: DataProcess

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class DataProcess(Process):
    def __init__(self, data_pipeline, **get_batch_kwargs):
        super(DataProcess, self).__init__(name='neuralnilm-data-process')
        self._stop = Event()
        self._queue = Queue(maxsize=3)
        self.data_pipeline = data_pipeline
        self._get_batch_kwargs = get_batch_kwargs

    def run(self):
        batch = self.data_pipeline.get_batch(**self._get_batch_kwargs)
        while not self._stop.is_set():
            try:
                self._queue.put(batch)
            except AssertionError:
                # queue is closed
                break
            batch = self.data_pipeline.get_batch(**self._get_batch_kwargs)

    def get_batch(self, timeout=30):
        if self.is_alive():
            return self._queue.get(timeout=timeout)
        else:
            raise RuntimeError("Process is not running!")

    def stop(self):
        self._stop.set()
        self._queue.close()
        self.terminate()
        self.join()
开发者ID:asez73,项目名称:neuralnilm,代码行数:31,代码来源:dataprocess.py

示例14: run_server_until_file_change

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
def run_server_until_file_change(serve_forever_path, verbosity=0):
    files_queue = Queue()
    file_changed = Event()

    worker = Worker(serve_forever_path, files_queue, os.getpid())
    worker.daemon = True

    monitor = Monitor(lambda: file_changed.set())
    monitor.start()
    worker.start()

    print("started server with PID %s" % worker.pid)

    while (not file_changed.is_set()) and worker.is_alive():
        try:
            path = files_queue.get(timeout=1)
        except queue.Empty:
            pass
        else:
            if path.endswith('testapp.py'):
                print("adding %s to paths" % path)
            monitor.add_path(path)

    monitor.stop()
    print("Waiting for monitor thread in %s to quit" % worker.pid)
    monitor.join()
    print("Waiting for server %s to quit" % worker.pid)
    worker.terminate()
    worker.join()
    print("server with PID %s done" % worker.pid)
开发者ID:olduvaihand,项目名称:wsgiwatcher,代码行数:32,代码来源:watcher.py

示例15: DeviceServer

# 需要导入模块: from multiprocessing import Event [as 别名]
# 或者: from multiprocessing.Event import is_set [as 别名]
class DeviceServer(ThreadedTCPServer, Process):
	
	#causes handle_request to return
	timeout = 1
	
	def __init__(self, mux, muxdevice, server_address, RequestHandlerClass):
		Process.__init__(self)
		ThreadedTCPServer.__init__(self, server_address, RequestHandlerClass)
		self.mux = mux
		self.muxdev = muxdevice
		self._stop = Event()

	def stop(self):
		self._stop.set()
		
	def stopped(self):
		return self._stop.is_set()

	def run(self):
		if self.stopped():
			_LOGGER.warning("Thread already stopped")
		
		while not self.stopped():
			self.handle_request()
		self.socket.close()
		_LOGGER.debug("%s will exit now" % (str(self)))
开发者ID:L45eMy,项目名称:Worker,代码行数:28,代码来源:deviceconnection.py


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