IBM Developer is your one-stop location for getting hands-on training and learning in-demand skills on relevant technologies such as generative AI, data science, AI, and open source.
ReadWriteLock は確実にロックを解除しないといけません。 readWriteLock.writeLock().lock(); try { // 何かの処理 } finally { readWriteLock.writeLock().unlock(); } いけませんが、よく忘れます。unlock() がコピペの魔術かなにかで lock() になったままとか。writeLock() をロックして readLock() をロック解除とか。 try-finally 句を使ってロックは確実にロック解除させるべきですが、ロックをかけるだけでコードが助長になります。 確実にリソースを閉じるときなど、ローンパターンを用いた using (with) 句がとても便利です。ということで、Scala で ReadWriteLock をするとき(あまり無いんじゃないかという話もありますが)、ローンパ
<Read-Write Lock> (1)ReentrantReadWriteLock Read-Write Lockは「読む処理」と「書く処理」に分けてロックを取ります。 「読む処理」X「書く処理」または「書く処理」X「書く処理」の時にはロックをかけますが 「読む処理」X「読む処理」の時にはロックをかけません。 読み込み処理が多い時または読み込み処理に時間がかかる場合など、一般的な排他制御より効率が期待されます。 java.util.concurrent.locks.ReentrantReadWriteLockクラスを使えば、Read-Write Lockの機能が容易に実現できます。 Read-Write Lockのパターンは lock try { 処理 } finally { unlock } のようにfinally節で必ずロックを開放しなければいけません。 ●Read-Write L
概要 Javaでは、マルチスレッド用のクラスとしてThreadが用意されている。[2017-04-15] しかし新しいJavaでは、Threadを直接使う事はあまり無い。 ExecutorService/Future(JDK 1.5)やFork/Join(JDK 1.7)といったクラスが追加されているので、そちらを使う。 Java21で仮想スレッド(virtual thread)が導入された。[2023-09-23] これにより、従来のスレッドはプラットフォームスレッド(platform thread)と呼ぶようになった。 Javaでは、マルチスレッド用のThreadクラスが用意されている。 Threadを継承して独自のクラスを作り、runメソッドをオーバーライドして処理本体を記述する。 class スレッドクラス名 extends Thread { @Override public vo
多数のTCP接続をハンドリングするサーバを書くなら、1コネクション1スレッドのモデルではなく、epollやkqueueのようなイベント駆動型のI/O多重化を行うべきだ、と言われます。だが、そのような主張は、「C10K問題」が書かれた2002年から7年経過した今でも有効なのでしょうか? echoサーバを書いて、ベンチマークを取ってみることにしました。 ふたつのグラフは、いずれも接続数とスループットの関係を表しています。最初のグラフは、全接続がアクティブに通信した場合、あとのグラフは、全接続のうち小数のコネクションが順次アクティブになっていく、というモデルです。これらのグラフから、以下ようなことが読み取れます。 epoll も per-thread モデルも、良くスケールする epoll は、ワークセットが小さい場合に (最大50%) per-thread モデルよりも高速 少なくとも、1コネ
命令型言語の Java や Ruby がユーザスレッドからカーネルスレッドに移行したのとは対照的に、関数型言語の Erlang や Haskell では軽量なユーザスレッドを提供することに成功しています。僕は、この違いが何から生じているのか理解したいと思っています。この記事では、これまで調べたことをまとめます。 軽量なユーザスレッドは Erlang が有名ですが、Haskell (GHC)でも利用できることを重ねて強調しておきます。Haskell の方が Erlang よりも速いようです。追記:フェアな比較ではないようなので、話半分で参照して下さい。 Rubyの場合 Ruby 1.8 まで提供されていたユーザスレッドは、軽量とは言えませんでした。その理由は、ユーザスレッドをコンテキストスイッチさせる際にスタックをコピーしていたからです。Rubyソースコード完全解説の第19章 スレッドによれ
8.1 スレッド状態 (thread state) とグローバルインタプリタロック (global interpreter lock) Python インタプリタは完全にスレッド安全 (thread safe) ではありません。 マルチスレッドの Python プログラムをサポートするために、グローバルな ロックが存在していて、現在のスレッドが Python オブジェクトに安全に アクセスする前に必ずロックを獲得しなければならなくなっています。 ロック機構がなければ、単純な操作でさえ、マルチスレッドプログラムの 実行に問題を引き起こす可能性があります: たとえば、二つのスレッドが 同じオブジェクトの参照カウントを同時にインクリメントすると、 結果的に参照カウントは二回でなく一回だけしかインクリメントされない かもしれません。 このため、グローバルインタプリタロックを獲得したスレッドだけが
はてなグループの終了日を2020年1月31日(金)に決定しました 以下のエントリの通り、今年末を目処にはてなグループを終了予定である旨をお知らせしておりました。 2019年末を目処に、はてなグループの提供を終了する予定です - はてなグループ日記 このたび、正式に終了日を決定いたしましたので、以下の通りご確認ください。 終了日: 2020年1月31日(金) エクスポート希望申請期限:2020年1月31日(金) 終了日以降は、はてなグループの閲覧および投稿は行えません。日記のエクスポートが必要な方は以下の記事にしたがって手続きをしてください。 はてなグループに投稿された日記データのエクスポートについて - はてなグループ日記 ご利用のみなさまにはご迷惑をおかけいたしますが、どうぞよろしくお願いいたします。 2020-06-25 追記 はてなグループ日記のエクスポートデータは2020年2月28
Python Memo以前に作った簡単なソケット通信を改造して、サーバー側の処理をスレッドに処理させることにする。こうすることで、複数のクライアントからの要求を同時に受け付けることができる。simpleserver.pyを改造したthreadserver.pyを作成する。clientは引き続きsimpleclient.pyを作成する。simpleserver.pyの実行結果を見ると、途中から新たなクライアントが接続してきているのが分かる。各スレッドは、クライアントから空白文字列を受信すると廃棄されるようにする。threadserver.py import socket, threading, time host = '' port = 15000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socke
Python のスレッドについてちょっと調べてみた. Javaだとselectがない(new IOを使うとそれっぽくは書けるが, すごく面倒) ので, スレッドがないと通信系のプログラムが書けないけど, Pythonにはちゃんとselectがあるので, これまであまり使ってこなかった. でも, やっぱりちゃんと勉強しておこうということで. Pythonにはスレッド用のモジュールとして, thread と threading の二つがある. 前者は非常に基本的なスレッドとロックオブジェクトを提供していて, 後者は前者を用いて構築された, より抽象度の高いスレッドライブラリ, という位置づけらしい. プログラマは基本的に後者だけ見てればいいのだろう. threadingが提供するオブジェクトは, 次の7つ. Thread:スレッド Lock:排他ロック RLock: リエントラントな排他ロック
multiprocessing の基本¶ サブプロセスを使用する最も簡単な方法は対象関数と共に Process オブジェクトをインスタンス化することで、その処理を開始させるために start() を呼び出してください。 import multiprocessing def worker(): """worker function""" print 'Worker' return if __name__ == '__main__': jobs = [] for i in range(5): p = multiprocessing.Process(target=worker) jobs.append(p) p.start()
みんなのIoT/みんなのPythonの著者。二子玉近く160平米の庭付き一戸建てに嫁/息子/娘/わんこと暮らしてます。月間1000万PV/150万UUのWebサービス運営中。 免責事項 プライバシーポリシー いやー,multiprocessingモジュールイイよ。パねえよ。要はプロセス間通信を行うときに便利なパッケージで,threadingと似たようなAPIなのでGILが回避できてマルチプロセッサとかマルチコアの性能を有効に使えてウハウハとか,リモートマシンにあるプロセスと通信したりオブジェクトをやりとりしたりできてウハウハとか,まあそういうモノです。これでおっきしない奴は技術者として大切な資質を失っていると思うよ。べつにおっきしなくてもいいんだけど,そういう奴とは一緒に仕事したくないよ。 ドキュメントのイントロダクションを超訳してみたです。 はじめに multiprocessingはth
経緯 最近書いているプログラムで無限ループするワーカースレッドを立てまくるものがあって, それを signal で安全に終了させる手段が知りたかった. 課題プログラム 以下のようなプログラムがあります. import threading import time def loopfunc(event): print "Thread Started" while not event.isSet(): time.sleep(1) print "Thread End" def main() : threads = [] e = threading.Event() for x in range(10): threads.append(threading.Thread(target=loopfunc, args=(e,))) threads[x].start() for th in threads:
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く