Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # file agent.py
- import threading
- import Queue
- import logging, sys
- from proxy import Proxy
- from subagents import Policeman, Consumer
- class Agent(object):
- def __init__(self, config):
- ...............
- # set other attributes
- self.todoQ = Queue.Queue(self.config.todoQ_size) # to do queue
- self.semaphore = threading.BoundedSemaphore(1) #
- ...............
- self.policeman = Policeman(self.config.policeman, self.todoQ, self.semaphore, self.doneQ, self.logger)
- self.consumer = Consumer(self.config.consumer, self.proxy, self.todoQ, self.logger)
- ...............
- def start(self):
- self.policeman.start()
- self.consumer.start()
- # file subagents.py
- import threading
- import traceback
- class Consumer(threading.Thread):
- def __init__(self, config, proxy, todoQ, logger):
- threading.Thread.__init__(self)
- ..............
- self.todoQ = todoQ
- ..............
- def run(self):
- while True:
- self.todoQ.put(self.proxy.get_task()) # self.proxy.get_task() restituire un dizionario e stampa sull'output "Get task XXXX"
- class Policeman(threading.Thread):
- def __init__(self, config, todoQ, semaphore, doneQ, logger):
- threading.Thread.__init__(self)
- ...............
- self.todoQ = todoQ
- self.semaphore = semaphore
- ...............
- def run(self):
- while True:
- with self.semaphore:
- task_dict = self.todoQ.get()
- task = Task(task_dict)
- task.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement