Stream-Framework
Stream-Framework copied to clipboard
How to use async?
If i don't set async=False for manager methods i always get [Errno 61] Connection refused error. And for some framework methods i cannot specify that unless i override bunch of framework methods. Is there a special setting? I have been going around the django example and could not see what i have been doing wrong. Any pointers would be appreciated. I am using django too.
Best regards
Edit I wrote in the begging async=True but it should have been aync=False
Not quite sure what could cause that. Do you have a snippet of the code you're running?
This is the follow logic
def follow(request, user_id, target_id):
try:
user = models.User.objects.get(id=user_id)
target = models.User.objects.get(id=target_id)
follow_obj = (filter(lambda x: x.target_id == target.id, user.follows))
if not follow_obj:
#User do not follow target yet
user.follows.add(user_types.Follow(target_id = target_id))
user.save()
share_manager.follow_user(user_id, target_id)
return get_success_response(user.name + " succesfully followed " + target.name)
else:
#User is already following target
return get_error_response(user.name + " is already following " + target.name)
except models.User.DoesNotExist:
return get_error_response("User or users do not exist")
Sharing
try:
u_id = int(user_id)
user = models.User.objects.get(id = u_id)
share_obj = user_types.Share(message = 'asdasd', verb_id=int(verb_id), id=1)
user.shares.add(share_obj)
user.save()
share_manager.share(user, share_obj)
return get_success_response("Succesfully shared")
except models.User.DoesNotExist:
return get_error_response("User does not exist")
Share manager looks like this
class ShareManager(Manager):
# this example has both a normal feed and an aggregated feed (more like
# how facebook or wanelo uses feeds)
feed_classes = dict(
normal=feeds.ShareFeed
)
user_feed_class = feeds.UserShareFeed
def share(self, user, share):
activity = user.create_activity(share)
# add user activity adds it to the user feed, and starts the fanout
self.add_user_activity(user.id, activity)
def remove_share(self, share):
activity = share.create_activity()
# removes the pin from the user's followers feeds
self.remove_user_activity(share.user_id, activity)
def get_user_follower_ids(self, user_id):
user = models.User.objects.get(id=user_id)
ids = [follow.target_id for follow in user.follows]
return {FanoutPriority.HIGH:ids}
share_manager = ShareManager()
This doesn't work. Both follow and share. If i change share_manager.follow_user(user_id, target_id)
to share_manager.follow_user(user_id, target_id, async=False)
follow works. If i return empty id array from def get_user_followr_ids(self, user_id)
share works to. Obviously it is not gonna appear on any followers feed. So i though i worked around this problem like this
user = models.User.objects.get(id=user_id)
target_ids = [follow.target_id for follow in user.follows]
share_manager.follow_many_users(user_id, target_ids, async=False)
feed = share_manager.get_feeds(user_id)['normal']
activities = feed[:100]
return activities
This works and i get the feed correct. However, i found out that this approaches gets pretty slow for large amount of data. If i make the share_manager.follow_many_users
async then i get the same error.
Regards