def fanout_newsfeeds_main_task(tweet_id, tweet_user_id): NewsFeed.objects.create(user_id=tweet_user_id, tweet_id=tweet_id) follower_ids = FriendshipService.get_follower_ids(tweet_user_id) index = 0 while index < len(follower_ids): batch_ids = follower_ids[index: index + FANOUT_BATCH_SIZE] fanout_newsfeeds_batch_task.delay(tweet_id, batch_ids) index += FANOUT_BATCH_SIZE return '{} newsfeeds are going to fanout, {} batches created.'.format( len(follower_ids), (len(follower_ids) - 1) // FANOUT_BATCH_SIZE + 1, )
def fanout_newsfeeds_main_task(tweet_id, tweet_user_id): # create newsfeed for tweet owner himself, so, he can see the tweet quickly NewsFeed.objects.create(user_id=tweet_user_id, tweet_id=tweet_id) # get the follower ids, split newsfeeds count based on batch size follower_ids = FriendshipService.get_follower_ids(tweet_user_id) index = 0 while index < len(follower_ids): batch_ids = follower_ids[index:index + FANOUT_BATCH_SIZE] fanout_newsfeeds_batch_task.delay(tweet_id, batch_ids) index += FANOUT_BATCH_SIZE return '{} newsfeeds going to fanout, {} batches created.'.format( len(follower_ids), (len(follower_ids) - 1) // FANOUT_BATCH_SIZE + 1, )
def fanout_newsfeeds_main_task(tweet_id, tweet_user_id): # 将推送给自己的newsfeed 率先创建, 确保自己能最快看到 NewsFeed.objects.create(user_id=tweet_user_id, tweet_id=tweet_id) #获得所有的 follower ids, 按照 batch size 拆分开 follower_ids = FriendshipService.get_follower_ids(tweet_user_id) index = 0 while index < len(follower_ids): batch_ids = follower_ids[index:index + FANOUT_BATCH_SIZE] fanout_newsfeeds_batch_task.delay(tweet_id, batch_ids) index += FANOUT_BATCH_SIZE return '{} newsfeeds going to fanout, {} batches created.'.format( len(follower_ids), (len(follower_ids) - 1) // FANOUT_BATCH_SIZE + 1, )
def fanout_newsfeeds_main_task(tweet_id, created_at, tweet_user_id): # import 写在里面避免循环依赖 from newsfeeds.services import NewsFeedService # 将推给自己的 Newsfeed 率先创建,确保自己能最快看到 NewsFeedService.create( user_id=tweet_user_id, tweet_id=tweet_id, created_at=created_at, ) # 获得所有的 follower ids,按照 batch size 拆分开 follower_ids = FriendshipService.get_follower_ids(tweet_user_id) index = 0 while index < len(follower_ids): batch_ids = follower_ids[index: index + FANOUT_BATCH_SIZE] fanout_newsfeeds_batch_task.delay(tweet_id, created_at, batch_ids) index += FANOUT_BATCH_SIZE return '{} newsfeeds going to fanout, {} batches created.'.format( len(follower_ids), (len(follower_ids) - 1) // FANOUT_BATCH_SIZE + 1, )
def fanout_newsfeeds_main_task(tweet_id, created_at, tweet_user_id): # import to prevent cycle dependence from newsfeeds.services import NewsFeedService # first create my own neesfeed # NewsFeed.objects.create(user_id=tweet_user_id, tweet_id=tweet_id) NewsFeedService.create( user_id=tweet_user_id, tweet_id=tweet_id, created_at=created_at, ) # get all follower ids, fanout in batch follower_ids = FriendshipService.get_follower_ids(tweet_user_id) index = 0 while index < len(follower_ids): batch_ids = follower_ids[index:index + FANOUT_BATCH_SIZE] fanout_newsfeeds_batch_task.delay(tweet_id, created_at, batch_ids) index += FANOUT_BATCH_SIZE return '{} newsfeeds going to fanout, {} batches created.'.format( len(follower_ids), (len(follower_ids) - 1) // FANOUT_BATCH_SIZE + 1, )
def fanout_newsfeeds_main_task(tweet_id, created_at, tweet_user_id): # we first create the newsfeed for the publisher, so that user can see the post # import 写在里面避免循环依赖 from newsfeeds.services import NewsFeedService # 将推给自己的 Newsfeed 率先创建,确保自己能最快看到 NewsFeedService.create( user_id=tweet_user_id, tweet_id=tweet_id, created_at=created_at, ) # retrieve all the follower id, and split into batches follower_ids = FriendshipService.get_follower_ids(tweet_user_id) index = 0 while index < len(follower_ids): batch_ids = follower_ids[index: index + FANOUT_BATCH_SIZE] fanout_newsfeeds_batch_task.delay(tweet_id, created_at, batch_ids) index += FANOUT_BATCH_SIZE return '{} newsfeeds going to fanout, {} batches created.'.format( len(follower_ids), (len(follower_ids) - 1) // FANOUT_BATCH_SIZE + 1, )