File: count-randomly.py

package info (click to toggle)
qpid-proton 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,632 kB
  • ctags: 20,083
  • sloc: java: 39,624; ansic: 29,389; python: 16,581; cpp: 11,250; ruby: 6,618; perl: 2,641; php: 1,033; xml: 957; sh: 230; pascal: 52; makefile: 32
file content (78 lines) | stat: -rwxr-xr-x 2,792 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from __future__ import print_function
import time, random
from proton.reactor import Reactor

# Let's try to modify our counter example. In addition to counting to
# 10 in quarter second intervals, let's also print out a random number
# every half second. This is not a super easy thing to express in a
# purely sequential program, but not so difficult using events.

class Counter:

    def __init__(self, limit):
        self.limit = limit
        self.count = 0

    def on_timer_task(self, event):
        self.count += 1
        print(self.count)
        if not self.done():
            event.reactor.schedule(0.25, self)

    # add a public API to check for doneness
    def done(self):
        return self.count >= self.limit

class Program:

    def on_reactor_init(self, event):
        self.start = time.time()
        print("Hello, World!")

        # Save the counter instance in an attribute so we can refer to
        # it later.
        self.counter = Counter(10)
        event.reactor.schedule(0.25, self.counter)

        # Now schedule another event with a different handler. Note
        # that the timer tasks go to separate handlers, and they don't
        # interfere with each other.
        event.reactor.schedule(0.5, self)

    def on_timer_task(self, event):
        # keep on shouting until we are done counting
        print("Yay, %s!" % random.randint(10, 100))
        if not self.counter.done():
            event.reactor.schedule(0.5, self)

    def on_reactor_final(self, event):
        print("Goodbye, World! (after %s long seconds)" % (time.time() - self.start))

# In hello-world.py we said the reactor exits when there are no more
# events to process. While this is true, it's not actually complete.
# The reactor exits when there are no more events to process and no
# possibility of future events arising. For that reason the reactor
# will keep running until there are no more scheduled events and then
# exit.
r = Reactor(Program())
r.run()