Skip to content

Commit b471daf

Browse files
author
Bob McElrath
committed
Adddress nits, use asyncio signal handling, create_task
1 parent 4bb7d1b commit b471daf

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

contrib/zmq/zmq_sub.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
# A blocking example using python 2.7 can be obtained from the git history:
7-
# https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
6+
"""
7+
ZMQ example using python3's asyncio
8+
9+
Bitcoin should be started with the command line arguments:
10+
bitcoind -testnet -daemon \
11+
-zmqpubhashblock=tcp://127.0.0.1:28332 \
12+
-zmqpubrawtx=tcp://127.0.0.1:28332 \
13+
-zmqpubhashtx=tcp://127.0.0.1:28332 \
14+
-zmqpubhashblock=tcp://127.0.0.1:28332
15+
16+
We use the asyncio library here. `self.handle()` installs itself as a
17+
future at the end of the function. Since it never returns with the event
18+
loop having an empty stack of futures, this creates an infinite loop. An
19+
alternative is to wrap the contents of `handle` inside `while True`.
20+
21+
A blocking example using python 2.7 can be obtained from the git history:
22+
https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
23+
"""
824

9-
import array
1025
import binascii
11-
import asyncio, zmq, zmq.asyncio
26+
import asyncio
27+
import zmq
28+
import zmq.asyncio
1229
import signal
1330
import struct
1431
import sys
@@ -55,16 +72,13 @@ async def handle(self) :
5572
asyncio.ensure_future(self.handle())
5673

5774
def start(self):
58-
asyncio.ensure_future(self.handle())
75+
self.loop.add_signal_handler(signal.SIGINT, self.stop)
76+
self.loop.create_task(self.handle())
5977
self.loop.run_forever()
6078

6179
def stop(self):
6280
self.loop.stop()
6381
self.zmqContext.destroy()
6482

6583
daemon = ZMQHandler()
66-
def signal_handler(num, frame):
67-
daemon.stop()
68-
exit(0)
69-
signal.signal(signal.SIGINT, signal_handler)
7084
daemon.start()

contrib/zmq/zmq_sub3.4.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,33 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
# A blocking example using python 2.7 can be obtained from the git history:
7-
# https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
6+
"""
7+
ZMQ example using python3's asyncio
8+
9+
Bitcoin should be started with the command line arguments:
10+
bitcoind -testnet -daemon \
11+
-zmqpubhashblock=tcp://127.0.0.1:28332 \
12+
-zmqpubrawtx=tcp://127.0.0.1:28332 \
13+
-zmqpubhashtx=tcp://127.0.0.1:28332 \
14+
-zmqpubhashblock=tcp://127.0.0.1:28332
15+
16+
We use the asyncio library here. `self.handle()` installs itself as a
17+
future at the end of the function. Since it never returns with the event
18+
loop having an empty stack of futures, this creates an infinite loop. An
19+
alternative is to wrap the contents of `handle` inside `while True`.
20+
21+
The `@asyncio.coroutine` decorator and the `yield from` syntax found here
22+
was introduced in python 3.4 and has been deprecated in favor of the `async`
23+
and `await` keywords respectively.
24+
25+
A blocking example using python 2.7 can be obtained from the git history:
26+
https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
27+
"""
828

9-
import array
1029
import binascii
11-
import asyncio, zmq, zmq.asyncio
30+
import asyncio
31+
import zmq
32+
import zmq.asyncio
1233
import signal
1334
import struct
1435
import sys
@@ -56,16 +77,13 @@ def handle(self) :
5677
asyncio.ensure_future(self.handle())
5778

5879
def start(self):
59-
asyncio.ensure_future(self.handle())
80+
self.loop.add_signal_handler(signal.SIGINT, self.stop)
81+
self.loop.create_task(self.handle())
6082
self.loop.run_forever()
6183

6284
def stop(self):
6385
self.loop.stop()
6486
self.zmqContext.destroy()
6587

6688
daemon = ZMQHandler()
67-
def signal_handler(num, frame):
68-
daemon.stop()
69-
exit(0)
70-
signal.signal(signal.SIGINT, signal_handler)
7189
daemon.start()

0 commit comments

Comments
 (0)