Exemple #1
0
    def main_run(self):
        # # # # Initial Executor object
        __executor = SimpleExecutor(mode=RunningMode.Asynchronous, executors=self.__Executor_Number)

        # # # # Running the Executor
        # # # # Asynchronous version of generally running
        __executor.run(
            function=self.__example.async_target_function,
            # function=self.__example.target_function,
            args=("index_1", "index_2.2"))

        # # # # Asynchronous version of generally running which will raise exception
        # __executor.run(
        #     function=self.__example.async_target_fail_function,
        #     args=("index_1", "index_2.2"))

        # # # # Map running which will raise exception
        # __executor.map(
        #     function=self.__example.async_target_function,
        #     args_iter=[("index_1", "index_2.2"), ("index_3",), (1, 2, 3)])

        # # # # Function version of map running which will raise exception
        # __executor.map_with_function(
        #     functions=[self.__example.async_target_function, self.__example.async_target_function],
        #     args_iter=[("index_1", "index_2.2"), ("index_3",), (1, 2, 3)])

        # # # # Get result
        __result = __executor.result()
        print("Result: ", __result)
Exemple #2
0
    def test_run(self, executor_as_thread: SimpleExecutor):

        TestSimpleExecutor._initial()

        def _target(*args, **kwargs):
            global Running_Count, Running_Thread_IDs, Running_PPIDs, Running_Current_Threads, Running_Finish_Timestamp

            with _Thread_Lock:
                Running_Count += 1

                _pid = os.getpid()
                _ppid = os.getppid()
                _ident = threading.get_ident()
                # _time = str(datetime.datetime.now())
                _time = int(time.time())

                Running_Thread_IDs.append(_ident)
                Running_PPIDs.append(_ppid)
                Running_Current_Threads.append(str(threading.current_thread()))
                Running_Finish_Timestamp.append(_time)

            time.sleep(Test_Function_Sleep_Time)
            return f"result_{threading.current_thread()}"

        executor_as_thread.run(function=_target)
        # Do some checking
        # 1. The amount of workers should be the same with the value of option *executors*.
        # 3. The amount of thread IDs should be the same with the value of option *executors*.
        # 2. The done-timestamp should be very close.
        TestSimpleExecutor._chk_run_record()
    def main_run(self):
        # Initialize Lock object
        __rlock = RLockFactory()

        # # # # Initial Executor object
        __executor = SimpleExecutor(mode=RunningMode.Parallel,
                                    executors=self.__Executor_Number)
        # __executor = SimpleExecutor(mode=RunningMode.Concurrent, executors=self.__Executor_Number)
        # __executor = SimpleExecutor(mode=RunningMode.GreenThread, executors=self.__Executor_Number)

        # # # # Running the Executor
        # # # # Generally running
        __executor.run(function=self.__example.target_function,
                       args=("index_1", "index_2.2"),
                       features=__rlock)

        # # # # Map running which will raise exception
        # __executor.map(
        #     function=self.__example.target_function,
        #     args_iter=[("index_1", "index_2.2"), ("index_3",), (1, 2, 3)],
        #     features=__rlock)

        # # # # Function version of map running which will raise exception
        # __executor.map_with_function(
        #     functions=[self.__example.target_function, self.__example.target_function],
        #     args_iter=[("index_1", "index_2.2"), ("index_3",), (1, 2, 3)],
        #     features=__rlock)

        # # # # Get result
        __result = __executor.result()
        print("Result: ", __result)
    def main_run(self):
        test_dao = TestDao(db_driver="mysql", use_pool=False)
        # test_dao = AsyncTestDao(db_driver="mysql", use_pool=False)

        # # # # Initial and instantiate feature object: Queue Task, Lock and Bounded Semaphore
        _queue_task = self.__init_queue()
        _features = self.__init_features()

        _executor = SimpleExecutor(
            mode=RunningMode.Parallel,
            # mode=RunningMode.Concurrent,
            # mode=RunningMode.GreenThread,
            # mode=RunningMode.Asynchronous,
            executors=self.__Worker_Number)

        _executor.run(function=test_dao.get_test_data,
                      queue_tasks=_queue_task,
                      features=_features)
        result = _executor.result()

        for r in result:
            print(f"+============ {r.worker_ident} =============+")
            print("Result.pid: ", r.pid)
            print("Result.worker_id: ", r.worker_ident)
            print("Result.worker_name: ", r.worker_name)
            print("Result.state: ", r.state)
            print("Result.data: ", r.data)
            print("Result.exception: ", r.exception)
            print("+====================================+\n")

        self.__save_to_files(result=result)

        end_time = time.time()
        self.__logger.info(
            f"Total taking time: {end_time - start_time} seconds")
Exemple #5
0
    def test_run(self, instantiate_adapter_executor: SimpleExecutor):
        TestSimpleExecutor._initial()

        instantiate_adapter_executor.run(function=target_function)
        _results = instantiate_adapter_executor.result()
        assert len(_results) == _Worker_Size, ""
        TestSimpleExecutor._chk_run_record(expected_size=Worker_Size)
    def main_run(self):
        # # # # Initial Executor object
        __executor = SimpleExecutor(mode=RunningMode.Asynchronous, executors=self.__Executor_Number)

        # # # # Running the Executor
        # # # # Generally running
        __executor.run(function=self.__example.crawl_process)

        # # # # Get result
        __result = __executor.result()
        print("Result: ", __result)
    def main_run(self):
        # # # # Initial Executor object
        __executor = SimpleExecutor(mode=RunningMode.Parallel, executors=self.__Executor_Number)
        # __executor = SimpleExecutor(mode=RunningMode.Concurrent, executors=self.__Executor_Number)
        # __executor = SimpleExecutor(mode=RunningMode.GreenThread, executors=self.__Executor_Number)

        # # # # Running the Executor
        # # # # Generally running
        __executor.run(function=self.__example.crawl_process)

        # # # # Get result
        __result = __executor.result()
        print("Result: ", __result)
Exemple #8
0
    def test_result(self, instantiate_adapter_executor: SimpleExecutor):

        def _target(*args, **kwargs):
            _current_worker = threading.current_thread()
            return f"result_{_current_worker}"

        instantiate_adapter_executor.run(function=_target)
        _results = instantiate_adapter_executor.result()
        assert len(_results) == _Worker_Size, ""
        for _r in _results:
            assert "result_" in _r.data, ""
            assert _r.worker_name, ""
            assert _r.worker_ident, ""
            assert _r.state, ""
            assert _r.pid, ""
            assert _r.exception is None, ""
# Import package multirunnable
import pathlib
import random
import time
import sys

package_path = str(pathlib.Path(__file__).parent.parent.parent.absolute())
sys.path.append(package_path)

from multirunnable import SimpleExecutor, RunningMode


def function(index):
    print(f"This isfunction with index {index}")
    time.sleep(3)
    return "Return Value"


executor = SimpleExecutor(mode=RunningMode.Parallel, executors=3)
executor.run(function=function,
             args={"index": f"test_{random.randrange(1, 10)}"})
result = executor.result()
print(f"This is final result: {result}")
Exemple #10
0
_Worker_Size = 7
_Semaphore_Value = 2


def target_function_with_bsmp() -> str:
    return retry_success_with_bsmp()


@retry.function
@RunWith.Bounded_Semaphore
def retry_success_with_bsmp() -> str:
    print("Running function ...")
    _worker_name = context.get_current_worker_name()
    return f"{_worker_name} Running Result"


if __name__ == '__main__':

    set_mode(RunningMode.Parallel)

    _bsmp_factory = BoundedSemaphoreFactory(value=_Semaphore_Value)
    _sexor = SimpleExecutor(executors=_Worker_Size)
    _sexor.run(function=target_function_with_bsmp, features=_bsmp_factory)
    _result = _sexor.result()

    for _r in _result:
        print("++++++++++++++++++++++++++++")
        print(f"worker_name: {_r.worker_name}")
        print(f"data: {_r.data}")
        print(f"exception: {_r.exception}")
import pathlib
import time
import sys

package_path = str(pathlib.Path(__file__).parent.parent.parent.absolute())
sys.path.append(package_path)

from multirunnable import SimpleExecutor, RunningMode
from multirunnable.api import RunWith
from multirunnable.factory import LockFactory

Thread_Number = 5


@RunWith.Lock
def lock_function():
    print("This is testing process with Lock and sleep for 3 seconds.")
    time.sleep(3)


if __name__ == '__main__':
    # Initialize Lock object
    __lock = LockFactory()

    # # # # Initial Executor object
    __executor = SimpleExecutor(mode=RunningMode.Concurrent,
                                executors=Thread_Number)

    # # # # Running the Executor
    __executor.run(function=lock_function, features=__lock)