Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use async? #209

Open
ghost opened this issue Mar 17, 2017 · 2 comments
Open

How to use async? #209

ghost opened this issue Mar 17, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Mar 17, 2017

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

@tschellenbach
Copy link
Owner

Not quite sure what could cause that. Do you have a snippet of the code you're running?

@ghost
Copy link
Author

ghost commented Mar 17, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant