@@ -1915,14 +1915,15 @@ In a similar way to the above section, we can implement a listener and handler
1915
1915
using `pynng <https://pypi.org/project/pynng/ >`_, which is a Python binding to
1916
1916
`NNG <https://nng.nanomsg.org/ >`_, billed as a spiritual successor to ZeroMQ.
1917
1917
The following snippets illustrate -- you can test them in an environment which has
1918
- ``pynng `` installed. Juat for variety, we present the listener first.
1918
+ ``pynng `` installed. Just for variety, we present the listener first.
1919
1919
1920
1920
1921
1921
Subclass ``QueueListener ``
1922
1922
^^^^^^^^^^^^^^^^^^^^^^^^^^
1923
1923
1924
1924
.. code-block :: python
1925
1925
1926
+ # listener.py
1926
1927
import json
1927
1928
import logging
1928
1929
import logging.handlers
@@ -1955,7 +1956,7 @@ Subclass ``QueueListener``
1955
1956
break
1956
1957
except pynng.Timeout:
1957
1958
pass
1958
- except pynng.Closed: # sometimes hit when you hit Ctrl-C
1959
+ except pynng.Closed: # sometimes happens when you hit Ctrl-C
1959
1960
break
1960
1961
if data is None :
1961
1962
return None
@@ -1988,6 +1989,7 @@ Subclass ``QueueHandler``
1988
1989
1989
1990
.. code-block :: python
1990
1991
1992
+ # sender.py
1991
1993
import json
1992
1994
import logging
1993
1995
import logging.handlers
@@ -2015,9 +2017,10 @@ Subclass ``QueueHandler``
2015
2017
2016
2018
logging.getLogger(' pynng' ).propagate = False
2017
2019
handler = NNGSocketHandler(DEFAULT_ADDR )
2020
+ # Make sure the process ID is in the output
2018
2021
logging.basicConfig(level = logging.DEBUG ,
2019
2022
handlers = [logging.StreamHandler(), handler],
2020
- format = ' %(levelname)-8s %(name)10s %(message)s ' )
2023
+ format = ' %(levelname)-8s %(name)10s %(process)6s %( message)s' )
2021
2024
levels = (logging.DEBUG , logging.INFO , logging.WARNING , logging.ERROR ,
2022
2025
logging.CRITICAL )
2023
2026
logger_names = (' myapp' , ' myapp.lib1' , ' myapp.lib2' )
@@ -2031,7 +2034,64 @@ Subclass ``QueueHandler``
2031
2034
delay = random.random() * 2 + 0.5
2032
2035
time.sleep(delay)
2033
2036
2034
- You can run the above two snippets in separate command shells.
2037
+ You can run the above two snippets in separate command shells. If we run the
2038
+ listener in one shell and run the sender in two separate shells, we should see
2039
+ something like the following. In the first sender shell:
2040
+
2041
+ .. code-block :: console
2042
+
2043
+ $ python sender.py
2044
+ DEBUG myapp 613 Message no. 1
2045
+ WARNING myapp.lib2 613 Message no. 2
2046
+ CRITICAL myapp.lib2 613 Message no. 3
2047
+ WARNING myapp.lib2 613 Message no. 4
2048
+ CRITICAL myapp.lib1 613 Message no. 5
2049
+ DEBUG myapp 613 Message no. 6
2050
+ CRITICAL myapp.lib1 613 Message no. 7
2051
+ INFO myapp.lib1 613 Message no. 8
2052
+ (and so on)
2053
+
2054
+ In the second sender shell:
2055
+
2056
+ .. code-block :: console
2057
+
2058
+ $ python sender.py
2059
+ INFO myapp.lib2 657 Message no. 1
2060
+ CRITICAL myapp.lib2 657 Message no. 2
2061
+ CRITICAL myapp 657 Message no. 3
2062
+ CRITICAL myapp.lib1 657 Message no. 4
2063
+ INFO myapp.lib1 657 Message no. 5
2064
+ WARNING myapp.lib2 657 Message no. 6
2065
+ CRITICAL myapp 657 Message no. 7
2066
+ DEBUG myapp.lib1 657 Message no. 8
2067
+ (and so on)
2068
+
2069
+ In the listener shell:
2070
+
2071
+ .. code-block :: console
2072
+
2073
+ $ python listener.py
2074
+ Press Ctrl-C to stop.
2075
+ DEBUG myapp 613 Message no. 1
2076
+ WARNING myapp.lib2 613 Message no. 2
2077
+ INFO myapp.lib2 657 Message no. 1
2078
+ CRITICAL myapp.lib2 613 Message no. 3
2079
+ CRITICAL myapp.lib2 657 Message no. 2
2080
+ CRITICAL myapp 657 Message no. 3
2081
+ WARNING myapp.lib2 613 Message no. 4
2082
+ CRITICAL myapp.lib1 613 Message no. 5
2083
+ CRITICAL myapp.lib1 657 Message no. 4
2084
+ INFO myapp.lib1 657 Message no. 5
2085
+ DEBUG myapp 613 Message no. 6
2086
+ WARNING myapp.lib2 657 Message no. 6
2087
+ CRITICAL myapp 657 Message no. 7
2088
+ CRITICAL myapp.lib1 613 Message no. 7
2089
+ INFO myapp.lib1 613 Message no. 8
2090
+ DEBUG myapp.lib1 657 Message no. 8
2091
+ (and so on)
2092
+
2093
+ As you can see, the logging from the two sender processes is interleaved in the
2094
+ listener's output.
2035
2095
2036
2096
2037
2097
An example dictionary-based configuration
0 commit comments