2022/04,江端さんの技術メモ

Golang で複数のgoroutineで、1つのchannelを使い回す場合(要求応答通信)をする際の注意点について

■Case 1

package main

import (
	"fmt"
	"sync"
)

//var Ch1 chan interface{}
var Ch1 chan interface{}

var mutex sync.Mutex
var wg sync.WaitGroup

func funcA(num int) {
	fmt.Println("Staring funcA No.", num)
	for i := 0; i < 10; i++ {
		mutex.Lock()
		fmt.Println("funcA:", num, " send", i+num*10)
		Ch1 <- i + num*10
		k := <-Ch1
		fmt.Println("				returned:", k)
		mutex.Unlock()
	}
	wg.Done()
}

func echo(num int) {
	fmt.Println("Staring echo No.", num)

	for {
		i := <-Ch1
		Ch1 <- i
	}

}

func main() {

	Ch1 = make(chan interface{}) // チャネルの初期化

	wg.Add(1)
	go funcA(1)

	wg.Add(1)
	go funcA(2)

	wg.Add(1)
	go echo(1)

	wg.Wait()
}

結果

funcA: 1  send 15
                                returned: 15
funcA: 1  send 16
                                returned: 16
funcA: 1  send 17
                                returned: 17
funcA: 1  send 18
                                returned: 18
funcA: 1  send 19
                                returned: 19
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x0)
        c:/go/src/runtime/sema.go:56 +0x25
sync.(*WaitGroup).Wait(0x483fc0)
        c:/go/src/sync/waitgroup.go:130 +0x71
main.main()
        C:/Users/ebata/goga/0-14/main.go:52 +0x131

goroutine 8 [chan receive]:
main.echo(0x0)
        C:/Users/ebata/goga/0-14/main.go:32 +0x8a
created by main.main
        C:/Users/ebata/goga/0-14/main.go:50 +0x125
exit status 2

■Case 2

# 最後に、close(Ch1)を入れてみた

        wg.Add(1)
	go echo(1)

	wg.Wait()
	close(Ch1)
}

結果

funcA: 2 send 25
                                returned: 25
funcA: 2  send 26
                                returned: 26
funcA: 2  send 27
                                returned: 27
funcA: 2  send 28
                                returned: 28
funcA: 2  send 29
                                returned: 29
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x0)
        c:/go/src/runtime/sema.go:56 +0x25
sync.(*WaitGroup).Wait(0x1e3fc0)
        c:/go/src/sync/waitgroup.go:130 +0x71
main.main()
        C:/Users/ebata/goga/0-14/main.go:52 +0x131

goroutine 8 [chan receive]:
main.echo(0x0)
        C:/Users/ebata/goga/0-14/main.go:32 +0x8a
created by main.main
        C:/Users/ebata/goga/0-14/main.go:50 +0x125
exit status 2

■Case 3

//wg.Add(1)
	go echo(1)

	wg.Wait()
	close(Ch1)
}

結果

funcA: 2  send 29
                                returned: 29
funcA: 1  send 17
                                returned: 17
funcA: 1  send 18
                                returned: 18
funcA: 1  send 19
                                returned: 19

C:\Users\ebata\goga\0-14>

結論

(1)channelを応答通信で使いたいのであれば、送信側(funcA)の中身をMutexでロックする必要がある。受信側は、一つだけならロックの必要はない
(2)送信側のgoroutineが消えると、受信側(echo)が『相手がいなくて寂しくなって』エラーを吐くので、chose(Ch1)で、きっちりchannelを殺しておくこと
(3)受信側(echo)が無限待ち"for()"であるなら、wg.Add(1)を発行してはならない。なぜならechoは、原則として終了しないgoroutineだから。

―― しかし、そこまで構わなくてもいいんじゃなかな、と思う。Golangは、相当"過保護"に作られていると思う。

以上

 

 

2022/04,江端さんの忘備録

現在着手中のプログラムが、完成間近になって、深刻な(面倒くさい)バグでスタックしています。

A program I am currently working on is stuck with a serious (and troublesome) bug as it nears completion.

と、まあ、このことは、これまでも散々書いてきたので、ご存知かと思いますが。

And, well, I'm sure you know this, as I have written about it extensively.

ただ、一つ言えることは、

One thing is for sure,

―― 在宅勤務がなかったら、絶対に着手できなかっただろう

"I never would have embarked on it if I didn't have telecommuting"

と確信できることです。

As such, I can be sure.

『連続して10時間以上、早朝までコードと格闘する』という贅沢が許されたのは、コロナ禍の恩恵と言えます。

It is a blessing of the Corona disaster that I was allowed the luxury of 'wrestling with the cord for more than 10 hours in a row, until the wee hours of the morning'.

-----

現在、このプログラムは"1-9-4"とか、ディレクトリ名で呼んでいます。

I currently refer to this program by the directory name "1-9-4".

晴れて完成(動作確認後)の暁には、このプログラムの開発コードを『コロナ』と改名しようかと考えています。

I am thinking of renaming the development code of this program "Corona" when it is finally completed (after confirming that it works).

憎悪と恩恵と入り混ったこの期間を、後世の『私』に伝える、最適なコードネームと確信します。

I am convinced that this is the best code name to convey to future generations of "me" this period of mixed feelings of hatred and beneficence.

未分類

嫁さんが、「U-NEXTのとあるコンテンツを見たい」と言ってきたので、U-NEXTについて色々調べてみました。

無料期間サービスの解約トラブルに関する事項が山のように出てきたので、『心証悪い』と思いました。

「月額プラン1490」を契約していた場合、契約から1年以内の解約に限り違約金5,000円が発生します。

を読んで、『今どき、コンテンツサービスで違約金?』と、その心証は、決定的となりました。

# 江端家は、大型連休中などに、1ヶ月単位でNetFlixへの登録→解約を繰返しています。

とは、言え、嫁さんの希望に応える為にも、入会してみました。

で、徹底的にそのコンテンツを捜してみたのですが、全く見付からなかったので、入会から30分後に解約手続を開始しました。

徹底解説!U-NEXTの解約・退会トラブルを回避するための全注意点

を見ながら、処理を進めたのですが、怖いと思ったのは、「解約」だけでは足りず、「退会」まで持っていかなければならなかった、ということです。

解約:サービスの停止はするが、アカウントは残る

退会:アカウントも消す

解約だけでは、メールによる宣伝やら、電話による営業までくらうらしいことが分かり、『やっぱり入会するんじゃなかった』と後悔しつつ、退会(アカウントも消す)まで持っていきました。

これが「解約」のエビデンス↓

これが「退会」のエビデンス↓

以上

2022/04,江端さんの忘備録

国際法違反の非戦闘員への攻撃の被害者であるウクライナの人の多くが、英語のインタビューに普通に応じているのを見て、驚いています。

I am surprised to watch many Ukrainians, victims of attacks on non-combatants in violation of international law, being interviewed normally in English.

非戦闘員である民間人が、(通訳を介さずに)自分の言葉で世界に主張できるかどうかは、情報戦において、物凄いインパクトがあると思うからです。

I believe that the ability of non-combatant civilians to assert their inhumanity to the world in their own words (without an interpreter) will have a tremendous impact in the information war.

我が国も、ウクライナと同様に、他国から侵略される可能性があります。

Our country, like Ukraine, could be invaded by another country.

# もっとも、我が国は、すでに国内に他国の軍事基地の駐留を許しているので、『侵略済み』とも言えますが、それはさておき。

# However, it could be said that our country has already been 'invaded' since we already allow other countries' military bases to be stationed in our country, but that is beside the point.

「侵略者の非道」を語る時に、世界に対して、事実上の世界言語である「英語」を使って語れるかどうかは、国防上、重要な事項ではないか、と思います。

When talking about the "atrocities of the aggressor," I think it is an important matter of national security to be able to speak to the world in English, the de facto world language.

しかし、我が国において、英語のリテラシーを持っている人間の比率は、江端試算で"4%"です。

However, the percentage of people in Japan who are literate in English is "4%" according to Ebata's estimate.

少ない。

Overwhelmingly low

英語のインタビューに対応できる日本人は、(私を含めて)は『いない』として考える方が良さそうです。

It would be better to assume that there are no Japanese (including me) who can handle an interview in English.

-----

今回の戦争では『情報戦のやり方』が変ってきているなぁ、と実感しています。

I feel that "the way of information warfare" is changing in this war.

これまでの情報戦は、国家政府のインテリジェンス(諜報機関)にる、プロパガンダやエスピオナージ(諜報活動)が中心でした。

Until now, information warfare has centered on propaganda and espionage by the intelligence agencies of national governments.

しかし、今回は、市井(しせい)の市民による、リアルタイム(あるいはライブ)の情報発信が、大きなインパクトになっていると思います。

This time, however, real-time (or live) information dissemination by citizens of the city has made a big impact.

戦争中の国際世論は勿論、戦後の戦争裁判の証拠が満載となる、という状況は、為政者に対してプレッシャーになると思います―― まあ、為政者にもよると思いますが。

I think that a situation where the evidence of the war trials after the war, not to mention international public opinion during the war, will be full of evidence, will put pressure on the rulers - well, it depends on the rulers, I guess.

(技術者としては、ブロックチェーン等を使った『証拠の保全と真正保証技術』が重要になってくるな、と思っています)

(As an engineer, I think "evidence preservation and authenticity assurance technology" using blockchain, etc. will become important.)

-----

「将来の侵略戦争に備えた英語の勉強」を、文部科学省が、英語教育の指導要領に書けるか、というと、ちょっと厳しいかな、とは思います。

It is a bit tough to say whether the Ministry of Education, Culture, Sports, Science and Technology (MEXT) can write "studying English in preparation for future wars of aggression" into the guidelines for English education.

となると、現場の教師の裁量ってことになるかなぁ。

So I guess it would be the discretion of the teachers in the field.

『ヘイ、ナンシー。今日はどこに行くんだい』

'Hey, Nancy. Where are you going today?"'

『あら、ボブ。今日はトムとデートよ』

'Oh, my God, Bob. Tom and I have a date today'

という会話レッスンの代わりに、

Instead of these conversation lessons,

『娘を返せ! 妻を返せ! 家を返せ! 土地を返せ! 俺たちが、お前らに一体何をしたというんだ!! 答えろ! ○○(敵国の為政者)!!』

'Give me back my daughter! Give me back my wife! Give me back my house and our land! What the hell have we done to you people! Answer me! XXXXX(The Enemy statesmen)!'

というフレーズの丸暗記だけでも良いかと思います。

I think it would be good to just memorize these phrases.

2022/04,江端さんの忘備録

ティーンエイジャの皆さん。

Dear my teenagers.

『何事も基礎が大切』という大人がいたら ―― その大人のことは、信じなくてもいいです。

If there is an adult who says, "The basics are important in everything" -- you don't have to believe that adult

絶対に、です。

ABSOLUTELY.

-----

私、今、ここ半年ばかりの時間を費やして作ってきた、プログラムの最終段階に入っています。

I am now in the final stages of a program that I have spent the last six months.

様々なプログラムをマージ(結合)させていくと、これまで出てこなかった問題が次々と出てきて、心を折られています。

As I merge (combine) the various programs, many problems appear with breaking my hearts.

その証拠として、今、私の体重は、『人間ドッグの当日に持ち込む体重』にまで、落ち込んでいます。

As proof, my weight is now down to 'the weight I bring on the day of my physical exam'.

-----

で、今思っていることは、

And now I'm thinking the following,

「もっと、排他処理の設計を緻密にすべきであった」

"The design of the exclusion process should have been more precise"

「もっと、Websocketの発行タイミングを慎重にコーディングすべきであった」

"The coded the timing of the Websocket issue should be more careful"

「当初から、今の設計方針が分かっていれば」

"If only this design policy had been in place from the beginning"

であり、

これは、一言で纏めると、

These can be summed up in one word.

『何事も基礎が大切』

"The basics are important in everything"

に帰着します。

-----

ところが、問題があります。

However, there is a problem.

『基礎が大切』であることの言葉の意味は、

The meanings of "The basics are important in everything" are,

(1)その『基礎』が何なのかが、スタート時には不明であること

(1) It is unclear at the start what that "basics" are.

(2)その『基礎』は、人によって、全くバラバラであること

(2) The "basics" are completely different from person to person.

です。

加えて、

in addition,

(3)そもそも、『基礎』は、これ以上もなく「退屈」で「苦痛」であること

(3) In the first place, "basics" are "boring" and "painful" without any more

(4)『基礎が大切』であるという言葉は、個々人の「後悔」から発せられていること

(4) The phrase "the basics are important" is emanating from the "regret" of each individual.

です。

-----

だから、

Therefore,

『私は、『基礎』を大切にしました。だから、何の問題もなく、今の私の成功があります』

"I took care of the 'basics'. That's why I am successful now, without any problems."

などと、語っている奴は、世の中に"一人もいない"のです(もしいたら、教えて下さい)。

There is "no one" in the world who says such a thing (if there is one, please let me know).

もし、本気でそれを言っているなら"単なるバカ"か、"低い目標を設定した低い志の人間"というだけの話です。

If they are really serious, they are either "simple idiots" or "people with low aspirations who set low goals.

いずれにしても、そのような人物は、ティーンエイジャの皆さんが、信用するに足る大人ではありません。

In any case, such a person is not an adult whom you teenagers can trust.

では、どのような大人なら信じていいか?

So what kind of adults can you trust?

―― 私は、このようにやって失敗して『基礎が大切』だと思ったけど、後の祭だったよ

"I failed at doing it this way and thought 'the basics are important', but it was just an 'It is no use crying over spilt milkl'"

とだけ、語って、その、自分だけの『基礎』を、あなたたちに押し付けることなく、話をストップできる勇気のある大人です。

An adult who has the courage to stop talking, and not impose his/her own personal "basics" on you

-----

という訳で、「基礎」なんて、ゴミです。

Therefore, "basics" are garbage.

それよりもっと大切なことは、「(1)始める勇気」であり、それを「(2)楽しむ工夫」と「(3)続ける工夫」だと思います。

What is more important is "(1) Courage to start" and "(2) Ingenuity to enjoy" and "(3) Ingenuity to continue".

この3つに関しては、誰もあなたに教えられません。

No one can tell you about these three.

自力で見つけていくしか、ありません。

You have to find it on your own.

-----

例えば、今読んで頂いている「英文を併記する日記」は、かなり覚悟がいりました。

For example, I was quite prepared for the "Diary with English text" that you are reading now.

特に、上記の「(1)始める勇気」は、本当に大変でしたよ。

Especially, the above "(1) Courage to start" was really hard.

おかげで、毎日「『基礎』は大切だったなぁ」を実感できています。

Thanks to this, I am able to realize every day that "the 'basics' WERE important.

未分類

1-9-4  bus(),person()の終了から、ちゃんと正常終了メッセージを送付して、echo2を自力破壊するようにしてみる。

デッドロック? している場所をつきとめる  1-9-4は完全に壊したっぽい

2022/04,江端さんの忘備録

―― 人の資格試験の合否を、メディアで公開していいのか?

"Is it no problem to publish a person's pass/fail on a qualifying exam in the media?"

と思うのですが、これを論理的に正当化できる理由を知っている人は教えて下さい。

I think it is a serious problem, however, if you know of a logical justification for this, please let me know.

そもそも、皇籍を離れた人は普通の民間人だと思うし、その配偶者は文句なしに一般人です。

To begin with, I believe that those who have left the imperial family are ordinary civilians, and their spouses are unquestionably civilians.

ならば、当然にプライバシーは守らなければなりません。

Then, of course, their privacy must be protected.

-----

以前、著名な政治家の子どもが起こした刑事事件の報道について、名誉毀損についての裁判が行われました。

There was a previous court case on defamation regarding the reporting of a criminal case involving the child of a prominent politician.

この時、司法は

At this time, I remember that the judiciary said

『著名な権力者の子どもは、その権力を継承する可能性があることから、公共性がある』

'Children of prominent authorities are public because they are likely to inherit their power.'

として、訴えを却下した、と覚えています。

and dismissed the suit.

私の知りたいのは、(その是非はともあれ)今回のケースにおける、上記の様なロジックです。

What I would like to know is the logic of the above in this case (regardless of the "pros" or "cons").

例えば、『皇籍離脱後に、皇籍復活の可能性がある』とか、そんな話です。

For example, 'there is a possibility of restoration of the imperial family after leaving the imperial family' or something like that.

-----

公共性を言うのであれば、

If you're talking about publicity.

「通常国会または臨時国会の開始2時間前の着席と、国会議員全員によるTOEICの受験」

"Seating two hours before the start of a regular or special session of the Diet and a TOEIC test taken by all members of the Diet."

の方が、はるかに公共性があると思います。

This is far more public.

■全ての国会議員のTOEICスコアの公開

- Publication of TOEIC scores for all members of Congress

■受験忌避者の議員資格の剥奪

- Disqualification of persons who avoid taking the examination

という条件付きで。

with the condition.

英語は事実上の国際言語であり、国会議員は、外交を担うフロントですから。

English is the de facto international language, and members of Congress are the front for diplomacy.

-----

ともあれ ――

Anyway--

犯罪者でもない限り、当人が『忘れて欲しい』と望むのであれば、私たちは『忘れる』ことに、協力的であっても良いと思います。

Unless the person is a criminal, we are willing to cooperate in "forgetting" if he or she wants us to forget.

2022/04,江端さんの技術メモ

1つのJavaScriptに2つのWebSocket繋げようとしたのですが、upgrade:websocket: request origin not allowed by Upgrader.CheckOrigin というエラーメッセージがでてきて、コネクションに失敗します。

で、よく分からんのですが、1行追加したら、直りました。

upgrader.Upgrade(w, r, nil)

の前に、

upgrader.CheckOrigin = func(r *http.Request) bool { return true }
を1行追加。
以下がサンプルコードです。
func echo2(w http.ResponseWriter, r *http.Request) {

	upgrader.CheckOrigin = func(r *http.Request) bool { return true }

	c, err := upgrader.Upgrade(w, r, nil) // cはサーバのコネクション
	if err != nil {
		log.Print("upgrade:", err)
		return
	}
	defer c.Close()

まあ、相変わらず、理由は分からんのですが、結果オーライです。

2022/04,江端さんの忘備録

江端家では、午後7時のNHKニュースを見る時間に合わせて、夕食となります。

At the Ebata household, dinner is timed to coincide with the 7 p.m. NHK news.

そして、最近、私は『食欲が低下』しています。

And recently, I have been 'losing my appetite'.

----

今回の、ロシアによるウクライナ侵攻は、

The Russian invasion of Ukraine teaches me, that

- 国際連合も、

- The United Nations,

- 国際条約も、

- International treaties,

- 国際司法裁判所も、

- the International Court of Justice,

- 主権の独立という考えや

- the principle of sovereign independence

- 正義という概念や、

- the concept of justice,

- 平和への希求や、

- the hope for peace,

- 様々な信仰も、

- various faiths,

つまるところ、『役に立たないんだ』ということを、改めて実感させてくれるものになりました。

After all, it became a reminder that 'those things are not useful'.

----

もちろん、全てのケースで『役に立たない』という訳ではないのでしょうが、私益に直結する問題

Of course, it may not be 'useless' in all cases, however, according to the matter directly related to private interests,

―― "生存権"という言葉の前では

"in the face of the word 'right to life'"

これらが、ほぼ100%踏み躙(ふみにじ)られ、効果を発揮できないことを、今更ながら再確認しました。

I now reconfirm that these are almost 100% ignored and ineffective.

『生き残ることが正義』『その為なら自分以外の人間の犠牲など物の数ではない』と開き直られたら、(少なくとも私は)反論できません。

If they say, "Survival is justice," or "The sacrifices of people other than myself are nothing compared to that," I can't argue with them (at least I can't).

―― 奴には権力(暴力装置(*))があって、私にはない

"He has the power, I don't"

(*)主に軍隊や警察などを指し、広義には政府、国家など強制力を持つ公権力全般

(*) Mainly refers to the military, police, etc., and in a broader sense, all public authorities with coercive power, such as the government and the state.

私(江端)は「正義」でも「平和」でもなく、ただ「弱い」だけです。

I (Ebata) am neither "justice" nor "peaceful". I am just "weak".

----

という訳で、

Therefore,

『「優しさ」や「思いやり」を社会に期待するのは、無駄だよ』

"Expecting 'kindness' and 'compassion' from society is futile"

と、子どもたちに教える教育にシフトした方が、子どもたちにとっては、親切なのかもしれないなぁ、

Maybe it would be kinder to the children if we shifted our education to teach them that way...

などと考えていると、食欲も落ちてくるのです。

Thinking about it makes me lose my appetite.

2022/04,江端さんの忘備録

私は使いませんでしたし、これからも使わないと思いますが ―― LINEのスタンプって、便利ですよね。

I didn't use it and I don't think I ever will -- LINE stamps are very useful.

これまで「言葉」でがんばってきた私の努力が、「無駄」だったとさえ思えます。

I even think that my efforts to date in "words" have been "in vain".

まあ、ここまで、「顔文字」も「絵文字」も使わずにがんばってきましたので、最後まで、この意地を ―― 根拠のない「意地」ですが ―― を通してみたいと思います。

Well, I have tried my best so far without using any "emoticons" or "emojis," and I would like to keep up my will until the end, even if it is an unfounded will.

-----

ところで、

By the way,

https://store.line.me/stickershop/product/4785055/ja のスタンプの製作者は、私(江端智一)ではありません。

I (Tomoichi Ebata) am not the creator of the stamp on https://store.line.me/stickershop/product/4785055/ja.

ただ、「私の普段の振舞い」を知っている人であれば、『江端智一が作った』と勘違いする内容のコンテンツとは思います。

However, I think that if people know "my usual behavior", they would misunderstand the content to be "created by Tomoichi Ebata".

しかし、私ではありません。私には、画才はありません。

But not me. I have no artistic talent.

-----

むしろ、このスタンプは、

Rather, this stamp

―― 江端(のコラム等)をディスる時に使える

"Can be used to disrespect Ebata's columns, etc."

と、思いました。

I think so.