Чтобы ответить на свой вопрос, вот как я решил проблему. Я хотел ограничить процессы, нагружающие процессор, по одному за раз. Итак, я создал глобальную очередь, добавил к ней, в противном случае я бы инициировал вызов с тяжелым процессором и создал непрерывную многопоточную функцию, которая обрабатывает очередь.
Initialize Queue
def init():
global crawl_queue
crawl_queue = Queue()
Process Очередь
def process_crawling_queue():
from flaskblog.books.routes import populate_from_amazon, send_async_mail, submit_books_to_websites
from flask_mail import Message
from flask import flash, url_for
while True:
size = settings.crawl_queue.qsize()
# print(f'Processes in queue: {size}')
if size:
p_name, *args = settings.crawl_queue.get()
if p_name == 'Populate_From_Amazon':
with app.app_context():
count = populate_from_amazon(*args, app=app)
user = args[0]
msg = Message(f'Finished Populating Books',
sender=app.config['MAIL_DEFAULT_SENDER'],
recipients=[user.email])
msg.html = f'{count} book(s) have been added/updated. <a href = {url_for("books.edit_books", _external=True)}> Edit them here</a> and then you can promote them.'
send_async_mail(app, msg)
elif p_name == "Submit_Books_To_Websites":
with app.app_context():
total_submissions = submit_books_to_websites(*args, app=app)
email = args[1]['email']
msg = Message(f'Finished Submitting Your Books',
sender=app.config['MAIL_DEFAULT_SENDER'],
recipients=[email])
msg.html = f'We have submitted your {len(args[0])} book(s) a total of {total_submissions} times. Good luck on the promotion! '
send_async_mail(app, msg)
else:
print(f'function {p_name} not recognized.')
time.sleep(3)
Добавить в очередь
data = ('Populate_From_Amazon', user)
crawl_queue.put(data)