FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/publish_subscribe.py at master · finiterecursion/python-patterns · GitHub
finiterecursion
/
python-patterns
Public
forked from
faif/python-patterns
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-patterns
/
publish_subscribe.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
92 lines (66 loc) · 1.98 KB
Breadcrumbs
python-patterns
/
publish_subscribe.py
Copy path
File metadata and controls
92 lines (66 loc) · 1.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Reference: http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
Author: https://github.com/HanWenfang
"""
class
Provider
:
def
__init__
(
self
):
self
.
msg_queue
=
[]
self
.
subscribers
=
{}
def
notify
(
self
,
msg
):
self
.
msg_queue
.
append
(
msg
)
def
subscribe
(
self
,
msg
,
subscriber
):
if
msg
not
in
self
.
subscribers
:
self
.
subscribers
[
msg
]
=
[]
self
.
subscribers
[
msg
].
append
(
subscriber
)
# unfair
else
:
self
.
subscribers
[
msg
].
append
(
subscriber
)
def
unsubscribe
(
self
,
msg
,
subscriber
):
self
.
subscribers
[
msg
].
remove
(
subscriber
)
def
update
(
self
):
for
msg
in
self
.
msg_queue
:
if
msg
in
self
.
subscribers
:
for
sub
in
self
.
subscribers
[
msg
]:
sub
.
run
(
msg
)
self
.
msg_queue
=
[]
class
Publisher
:
def
__init__
(
self
,
msg_center
):
self
.
provider
=
msg_center
def
publish
(
self
,
msg
):
self
.
provider
.
notify
(
msg
)
class
Subscriber
:
def
__init__
(
self
,
name
,
msg_center
):
self
.
name
=
name
self
.
provider
=
msg_center
def
subscribe
(
self
,
msg
):
self
.
provider
.
subscribe
(
msg
,
self
)
def
run
(
self
,
msg
):
print
(
"{} got {}"
.
format
(
self
.
name
,
msg
))
def
main
():
message_center
=
Provider
()
fftv
=
Publisher
(
message_center
)
jim
=
Subscriber
(
"jim"
,
message_center
)
jim
.
subscribe
(
"cartoon"
)
jack
=
Subscriber
(
"jack"
,
message_center
)
jack
.
subscribe
(
"music"
)
gee
=
Subscriber
(
"gee"
,
message_center
)
gee
.
subscribe
(
"movie"
)
fftv
.
publish
(
"cartoon"
)
fftv
.
publish
(
"music"
)
fftv
.
publish
(
"ads"
)
fftv
.
publish
(
"movie"
)
fftv
.
publish
(
"cartoon"
)
fftv
.
publish
(
"cartoon"
)
fftv
.
publish
(
"movie"
)
fftv
.
publish
(
"blank"
)
message_center
.
update
()
if
__name__
==
"__main__"
:
main
()
### OUTPUT ###
# jim got cartoon
# jack got music
# gee got movie
# jim got cartoon
# jim got cartoon
# gee got movie
Back
|
FazBrowse Home
|
New Git URL