Python
- 7 ответов
- 0 вопросов
6
Вклад в тег
def say_hello(name):
print 'Hello,', name
def say_goodbye(name):
print 'Bye,', name
phrases = {
'hello': say_hello,
'bye': say_goodbye,
}
def say(what, name):
phrases[what](name)
say('hello', 'Ivan')
say('bye', 'Oleg')
os.open
: https://docs.python.org/2/library/os.html#os.open, скорее всего вам необходимо открывать файл с флагом O_NONBLOCK, больше информации здесь.from Queue import Queue
from threading import Thread
def scrape(link):
conn = sqlite3.connect('db.sqlite3')
curs = conn.cursor()
# insert link, close connection
class Worker(Thread):
def __init__(self, tasks):
super(Worker, self).__init__()
self.tasks = tasks
self.daemon = True
def run(self):
while True:
link = self.tasks.get()
try:
scrape(link)
finally:
self.tasks.task_done()
if __name__ == '__main__':
# максимальное количество одновременных потоков
capacity = 0 # infinite
queue = Queue(capacity)
workers = 3
for _ in range(workers):
Worker(queue).start()
for link in select.fetchall():
queue.put(link)
queue.join()
print 'Done'