diff options
author | ngharo <ngharo@gmail.com> | 2018-06-29 23:37:52 -0500 |
---|---|---|
committer | ngharo <ngharo@gmail.com> | 2018-06-29 23:37:52 -0500 |
commit | cd1072f2aa51157112a9d210f7144cec5a762171 (patch) | |
tree | 3bbe3d7ce3c8da0739e01bbef2aff8adb5929c41 /shared.py | |
parent | 49c83f0c693890f1e2df33919606362890bf726d (diff) | |
download | ansibots-rewrite.tar.xz ansibots-rewrite.zip |
WIPrewrite
Diffstat (limited to 'shared.py')
-rw-r--r-- | shared.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/shared.py b/shared.py new file mode 100644 index 0000000..3fe6a4c --- /dev/null +++ b/shared.py @@ -0,0 +1,59 @@ +import sys +import queue +import pydle +import logging +import time +from lib import worker + +num_worker_threads = 2 +password = 'xxx' + +class ansibot(pydle.Client): + def on_connect(self): + self.join('#test99') + + def on_message(self, source, target, message): + if message != "!spam": + return + + global dispatched + global q + + """ + Here I thought I could have one client pickup and dispatch + work onto a queue. + + Clients then pick items off the queue send it along as a message to + the channel. + + What happens is the first client gobbles up the entire queue. I'm thinking + that client on_message callbacks are dispatched in serial. Am i right? + + This kinda thing works in testing using threading in place of ClientPool(). + Any pointers? + """ + if not dispatched: + dispatched = True + # generate some some data to push on the queue + [q.put(str(x)) for x in range(10)] + + # go to work on data + while not q.empty(): + part = q.get() + + self.message(source, part) + # give time for another client to grab items off the queue + # (theoretically) + time.sleep(0.1) + q.task_done() + +q = queue.Queue() +dispatched = False + +pool = pydle.ClientPool() +for i in range(num_worker_threads): + client = ansibot('zz' + str(i)) + pool.connect(client, 'irc.foo.bar', 7001, password=password, tls=True, tls_verify=False) + +# This will make sure all clients are treated in a fair way priority-wise. +pool.handle_forever() |