Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

python - Multiprocessing.Pool runs only one process

I have a Django project and I must fill/update the database with data from an external API.

For every user, I must update the values for every minute of every day fetched by the external API. As you understand this is a time-consuming process if I pass multiple months.

Please note that I make a call for each day, and process the data of the fetched json. After a specific number of requests, I must wait one hour.

With all that in mind, I prepared a script that did all the work but it was taking ages to complete so I decided to proceed with multiprocessing.

There is a method user_sync(day_list, user) that handles everything and works perfectly.

and here are the lines of code that initiate multiprocessing.

user_list = User.objects.filter(username__startswith='US')

start_date = datetime.datetime(2020, 6, 28, 0, 0, tzinfo=pytz.UTC)
end_date = datetime.datetime(2021, 1, 20, 0, 0, tzinfo=pytz.UTC)

day_list = create_day_list(start_date, end_date)
pool = multiprocessing.Pool()
try:
    part = partial(user_sync, day_list)
    pool.map(part, user_list)
    pool.join()
except Exception as e:
    print(e)
    pool.close()
finally:
    pool.close()
    print('FINISHED')

To my understanding, it should run multiple user_sync methods for every user in parallel but it doesn't

Any help or point to the right direction would be much appreciated.

question from:https://stackoverflow.com/questions/65834964/multiprocessing-pool-runs-only-one-process

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...