コード例 #1
0
def _get_worker_id(use_adapter_context: bool = False) -> str:
    """
    Get the identity of current worker instance.

    :param use_adapter_context:
    :return:
    """

    _rmode = get_current_mode(force=True)

    if use_adapter_context is True:
        _worker_id = _adapter_context.get_current_worker_ident()
    else:
        if _rmode is RunningMode.Parallel:
            _worker_id = mp.current_process().ident
        elif _rmode is RunningMode.Concurrent:
            _worker_id = threading.current_thread().ident
        elif _rmode is RunningMode.GreenThread:
            _worker_id = gevent.threading.get_ident()
        elif _rmode is RunningMode.Asynchronous:
            if (PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION) > (3, 6):
                _worker_id = id(asyncio.current_task())
            else:
                _worker_id = id(asyncio.Task.current_task())
        else:
            raise ValueError("The RunningMode has the unexpected mode.")

    return str(_worker_id)
コード例 #2
0
def initial_lock(use_adapter_factory: bool = False):
    """
    Instantiate Lock object. It would initial via context of multirunnable if
    option *use_adapter_factory* is True, or it  would initial by their own native
    library or third party library.

    :param use_adapter_factory:
    :return:
    """

    global _Worker_Lock

    _rmode = get_current_mode(force=True)

    if use_adapter_factory is True:
        _lock_factory = LockFactory()
        _lock_factory.feature_mode = _rmode.value["feature"]
        _Worker_Lock = _lock_factory.get_instance()
    else:
        if _rmode is RunningMode.Parallel:
            _Worker_Lock = mp.Lock()
        elif _rmode is RunningMode.Concurrent:
            _Worker_Lock = threading.Lock()
        elif _rmode is RunningMode.GreenThread:
            _Worker_Lock = gevent.threading.Lock()
        elif _rmode is RunningMode.Asynchronous:
            if (PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION) >= (3, 10):
                _Worker_Lock = asyncio.locks.Lock()
            else:
                _Worker_Lock = asyncio.locks.Lock(loop=asyncio.get_event_loop())
                # _Worker_Lock = asyncio.locks.Lock()
        else:
            raise ValueError("The RunningMode has the unexpected mode.")

    return _Worker_Lock
コード例 #3
0
def initial_rlock(use_adapter_factory: bool = False):
    """
    It's same as function 'initial_lock' but this function instantiates RLock object.

    :param use_adapter_factory:
    :return:
    """

    global _Worker_RLock

    _rmode = get_current_mode(force=True)

    if use_adapter_factory is True:
        _rlock_factory = RLockFactory()
        _rlock_factory.feature_mode = _rmode.value["feature"]
        _Worker_RLock = _rlock_factory.get_instance()
    else:
        if _rmode is RunningMode.Parallel:
            _Worker_RLock = mp.RLock()
        elif _rmode is RunningMode.Concurrent:
            _Worker_RLock = threading.RLock()
        elif _rmode is RunningMode.GreenThread:
            _Worker_RLock = gevent.lock.RLock()
        else:
            raise ValueError("The RunningMode has the unexpected mode.")

    return _Worker_RLock
コード例 #4
0
    def test_saving_with_one_worker_one_file(self, file_saver: FileSaver):
        _mediator = SavingMediator()
        file_saver.register(mediator=_mediator,
                            strategy=SavingStrategy.ONE_THREAD_ONE_FILE)

        if get_current_mode(force=True) is RunningMode.Parallel:
            _worker = _TestProcess(file_saver)
        else:
            _worker = _TestThread(file_saver)
        _worker.start()
        _worker.join()

        _result = file_saver.save(file="no.no",
                                  mode="yee",
                                  encoding="UTF-8",
                                  data=Test_Data_List)
        assert _result == _Do_Nothing_Flag, ""

        global Global_Run_Flags, Run_Procedure_List
        assert Global_Run_Flags[
            "Run_Open_Process_Flag"] is True, "It should experience 'open' process."
        assert Global_Run_Flags[
            "Run_Write_Process_Flag"] is True, "It should experience 'write' process."
        assert Global_Run_Flags[
            "Run_Close_Process_Flag"] is True, "It should experience 'close' process."
        assert Run_Procedure_List[
            0] == "open", "It should run 'open' process first."
        assert Run_Procedure_List[
            1] == "write", "It should run 'write' process second."
        assert Run_Procedure_List[
            2] == "close", "It should run 'close' process finally."
コード例 #5
0
    def test_initial_running_strategy(self, instantiate_executor: SimpleExecutor):
        instantiate_executor._initial_running_strategy()

        from multirunnable.executor import General_Runnable_Strategy
        assert General_Runnable_Strategy is not None, "It should be assign running-strategy instance."
        _rmode = get_current_mode(force=True)
        if _rmode is RunningMode.Parallel:
            assert isinstance(General_Runnable_Strategy, ProcessStrategy), "It should be an sub-instance of 'ProcessStrategy'."
        elif _rmode is RunningMode.Concurrent:
            assert isinstance(General_Runnable_Strategy, ThreadStrategy), "It should be an sub-instance of 'ThreadStrategy'."
        elif _rmode is RunningMode.Parallel.GreenThread:
            assert isinstance(General_Runnable_Strategy, GreenThreadStrategy), "It should be an sub-instance of 'GreenThreadStrategy'."
        elif _rmode is RunningMode.Asynchronous:
            assert isinstance(General_Runnable_Strategy, AsynchronousStrategy), "It should be an sub-instance of 'AsynchronousStrategy'."
        else:
            raise ValueError("The RunningMode has the unexpected mode.")
コード例 #6
0
def _sleep_time() -> None:
    """
    Sleep by the RunningMode.

    :return:
    """

    _rmode = get_current_mode(force=True)

    if _rmode is RunningMode.Asynchronous:
        raise RuntimeError("This function doesn't support 'RunningMode.Asynchronous'.")

    if _rmode is RunningMode.Parallel or _rmode is RunningMode.Concurrent:
        time.sleep(Test_Function_Sleep_Time)
    elif _rmode is RunningMode.GreenThread:
        gevent.sleep(Test_Function_Sleep_Time)
    else:
        raise ValueError("The RunningMode has the unexpected mode.")
コード例 #7
0
    def test_initial_running_strategy(self, simple_pool: SimplePool):
        simple_pool._initial_running_strategy()

        _rmode = get_current_mode(force=True)

        from multirunnable.pool import Pool_Runnable_Strategy
        if _rmode is RunningMode.Parallel:
            assert Pool_Runnable_Strategy is not None, "It should be assign running-strategy instance."
            assert isinstance(
                Pool_Runnable_Strategy, ProcessPoolStrategy
            ), "It should be an sub-instance of 'ProcessPoolStrategy'."
        elif _rmode is RunningMode.Concurrent:
            assert Pool_Runnable_Strategy is not None, "It should be assign running-strategy instance."
            assert isinstance(
                Pool_Runnable_Strategy, ThreadPoolStrategy
            ), "It should be an sub-instance of 'ThreadPoolStrategy'."
        elif _rmode is RunningMode.GreenThread:
            assert Pool_Runnable_Strategy is not None, "It should be assign running-strategy instance."
            assert isinstance(
                Pool_Runnable_Strategy, GreenThreadPoolStrategy
            ), "It should be an sub-instance of 'GreenThreadPoolStrategy'."
        else:
            raise ValueError("The RunningMode has the unexpected mode.")