Giter Site home page Giter Site logo

pylatch's Introduction

pylatch

Java や C# に存在する CountDownLatch を Python で簡易実装したものです。

(C# の場合は、 CountDownEvent がそれに当たる)

マルチスレッド版とマルチプロセス版があります。

どちらもクラス名は CountDownLatch です。

使い方 (マルチスレッド用)

利用するクラスは pylatch.threadlatch.CountDownLatch となります。

>>> # ---------------------------------------------
>>> # スレッド処理で利用する場合
>>> # ---------------------------------------------
>>> import threading as th
>>> import pylatch.threadlatch as pl

>>> latch = pl.CountDownLatch(2)
>>> th1 = th.Thread(target=lambda: latch.count_down())
>>> th2 = th.Thread(target=lambda: latch.count_down())

>>> latch.count
2
>>> latch.await(timeout=1.0)
False

>>> th1.start()
>>> th1.join()
>>> latch.count
1
>>> latch.await(timeout=1.0)
False

>>> th2.start()
>>> th2.join()
>>> latch.count
0
>>> latch.await(timeout=1.0)
True

使い方 (マルチプロセス用)

利用するクラスは pylatch.processlatch.CountDownLatch となります。

>>> # ---------------------------------------------
>>> # マルチプロセス処理で利用する場合
>>> # ---------------------------------------------
>>> import multiprocessing as mp
>>> import pylatch.processlatch as pl

>>> latch = pl.CountDownLatch(2)
>>> proc1 = mp.Process(target=pl.__for_doctest, args=(latch,))
>>> proc2 = mp.Process(target=pl.__for_doctest, args=(latch,))

>>> latch.count
2
>>> latch.await(timeout=1.0)
False

>>> proc1.start()
>>> proc1.join()
>>> latch.count
1
>>> latch.await(timeout=1.0)
False

>>> proc2.start()
>>> proc2.join()
>>> latch.count
0
>>> latch.await(timeout=1.0)
True

テスト

$ python -m unittest

参考情報

以下の情報を参考にさせていただきました。

pylatch's People

Contributors

devlights avatar

Watchers

James Cloos avatar  avatar  avatar

pylatch's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.