SHOW:
|
|
- or go back to the newest paste.
| 1 | # file agent.py | |
| 2 | import threading | |
| 3 | import Queue | |
| 4 | import logging, sys | |
| 5 | ||
| 6 | from proxy import Proxy | |
| 7 | from subagents import Policeman, Consumer | |
| 8 | ||
| 9 | class Agent(object): | |
| 10 | def __init__(self, config): | |
| 11 | ||
| 12 | ............... | |
| 13 | # set other attributes | |
| 14 | self.todoQ = Queue.Queue(self.config.todoQ_size) # to do queue | |
| 15 | self.semaphore = threading.BoundedSemaphore(1) # | |
| 16 | ............... | |
| 17 | self.policeman = Policeman(self.config.policeman, self.todoQ, self.semaphore, self.doneQ, self.logger) | |
| 18 | self.consumer = Consumer(self.config.consumer, self.proxy, self.todoQ, self.logger) | |
| 19 | ............... | |
| 20 | ||
| 21 | def start(self): | |
| 22 | self.policeman.start() | |
| 23 | self.consumer.start() | |
| 24 | ||
| 25 | # file subagents.py | |
| 26 | import threading | |
| 27 | import traceback | |
| 28 | ||
| 29 | class Consumer(threading.Thread): | |
| 30 | def __init__(self, config, proxy, todoQ, logger): | |
| 31 | threading.Thread.__init__(self) | |
| 32 | ||
| 33 | .............. | |
| 34 | self.todoQ = todoQ | |
| 35 | .............. | |
| 36 | ||
| 37 | def run(self): | |
| 38 | while True: | |
| 39 | self.todoQ.put(self.proxy.get_task()) # self.proxy.get_task() restituire un dizionario e stampa sull'output "Get task XXXX" | |
| 40 | ||
| 41 | class Policeman(threading.Thread): | |
| 42 | def __init__(self, config, todoQ, semaphore, doneQ, logger): | |
| 43 | threading.Thread.__init__(self) | |
| 44 | ............... | |
| 45 | self.todoQ = todoQ | |
| 46 | self.semaphore = semaphore | |
| 47 | ............... | |
| 48 | ||
| 49 | ||
| 50 | def run(self): | |
| 51 | while True: | |
| 52 | with self.semaphore: | |
| 53 | task_dict = self.todoQ.get() | |
| 54 | task = Task(task_dict) | |
| 55 | - | task.start() |
| 55 | + | task.start() # dura almeno 10 secondi |