def main():
    #  print(sys.version)
    #  スレッドベースの非同期実行
    future = ThreadPoolExecutor().submit(f, 'wef')
    is_feature = isinstance(future, Future)
    print(is_feature)

    result = future.result()
    print(result)

    is_running = future.running()
    print(is_running)

    is_done = future.done()
    print(is_done)

    is_cancelled = future.cancelled()
    print(is_cancelled)

    #  print(download(urls[0]))
    get_sequentials()
    get_multi_thread()

    counter = Counter()
    threads = 2
    with ThreadPoolExecutor() as e:
        #  2つのスレッドを用意し、それぞれcount_upを呼び出す
        futures = [e.submit(count_up, counter) for _ in range(threads)]
        done, not_done = wait(futures)
        #  print(done, not_done)

    print(f'{counter.count} != 2000000')


    counter2 = ThreadSafeCounter()
    threads = 2
    with ThreadPoolExecutor() as e:
        #  2つのスレッドを用意し、それぞれcount_upを呼び出す
        futures = [e.submit(count_up, counter2) for _ in range(threads)]
        done, not_done = wait(futures)
        print(done, not_done)

    print(f'{counter2.count} == 2000000')


    with ThreadPoolExecutor(max_workers=3) as e:
        d = Downloader()
        futures = [e.submit(d.get(url)) for url in urls]
        for future in as_completed(futures):
            #  r = future.result()
            print(future)




    c = MyClass()
    c.execute()
from concurrent.futures import (ThreadPoolExecutor, Future)


def func():
    return 1


if __name__ == '__main__':
    # 非同期に行いたい処理をsubmitに渡すと、
    # その処理の実行がスケジューリングされて
    # Futureクラスのインスタンスが返される。
    # ThreadPoolExecutorはスレッドベースの非同期実行
    future = ThreadPoolExecutor().submit(func)
    print(isinstance(future, Future))

    # futureはスケジューリングされた呼び出し可能オブジェクトを
    # カプセル化したもので、実行が先送りされている事を表現している。
    # スケジューリングはconcurrent.futuresモジュールが行う。

    # 非同期で実行した処理の戻り値を取得
    print(future.result())

    # 現在の状態を確認する
    print(future.done())
    print(future.running())
    print(future.cancelled())
Exemplo n.º 3
0
    def _build(self, build_dir, cancel_event, lock: lockfile.LockFile = None):
        """Perform the build. This assumes there actually is a build to perform.
        :param Path build_dir: The directory in which to perform the build.
        :param threading.Event cancel_event: Event to signal that the build
            should stop.
        :param lock: The lockfile object. This will need to be refreshed to
            keep it from expiring.
        :returns: True or False, depending on whether the build appears to have
            been successful.
        """

        try:
            future = ThreadPoolExecutor().submit(self._setup_build_dir,
                                                 build_dir)
            while future.running():
                time.sleep(lock.SLEEP_PERIOD)
                lock.renew()
            # Raise any errors raised by the thread.
            future.result()
        except TestBuilderError as err:
            self.tracker.error(
                note=("Error setting up build directory '{}': {}".format(
                    build_dir, err)))
            return False

        try:
            # Do the build, and wait for it to complete.
            with self.tmp_log_path.open('w') as build_log:
                # Build scripts take the test id as a first argument.
                cmd = [self._script_path.as_posix(), str(self.test.id)]
                proc = subprocess.Popen(cmd,
                                        cwd=build_dir.as_posix(),
                                        stdout=build_log,
                                        stderr=build_log)

                result = None
                timeout = self._timeout
                while result is None:
                    try:
                        result = proc.wait(timeout=1)
                    except subprocess.TimeoutExpired:
                        lock.renew()
                        if self._timeout_file.exists():
                            timeout_file = self._timeout_file
                        else:
                            timeout_file = self.tmp_log_path

                        try:
                            timeout = max(
                                timeout,
                                timeout_file.stat().st_mtime + self._timeout)
                        except OSError:
                            pass

                        # Has the output file changed recently?
                        if time.time() > timeout:
                            # Give up on the build, and call it a failure.
                            proc.kill()
                            cancel_event.set()
                            self.tracker.fail(
                                state=STATES.BUILD_TIMEOUT,
                                note="Build timed out after {} seconds.".
                                format(self._timeout))
                            return False

                        if cancel_event is not None and cancel_event.is_set():
                            proc.kill()
                            self.tracker.update(
                                state=STATES.ABORTED,
                                note="Build canceled due to other builds "
                                "failing.")
                            return False

        except subprocess.CalledProcessError as err:
            if cancel_event is not None:
                cancel_event.set()
            self.tracker.error(
                note="Error running build process: {}".format(err))
            return False

        except (IOError, OSError) as err:
            if cancel_event is not None:
                cancel_event.set()

            self.tracker.error(
                note="Error that's probably related to writing the "
                "build output: {}".format(err))
            return False
        finally:
            try:
                self.tmp_log_path.rename(build_dir / self.LOG_NAME)
            except OSError as err:
                self.tracker.warn(
                    "Could not move build log from '{}' to final location "
                    "'{}': {}".format(self.tmp_log_path, build_dir, err))

        try:
            self._fix_build_permissions(build_dir)
        except OSError as err:
            self.tracker.warn("Error fixing build permissions: %s".format(err))

        if result != 0:
            if cancel_event is not None:
                cancel_event.set()
            self.tracker.fail(note="Build returned a non-zero result.")
            return False
        else:

            self.tracker.update(state=STATES.BUILD_DONE,
                                note="Build completed successfully.")
            return True