{"action":"created","issue":{"url":"https://api.github.com/repos/nameko/nameko/issues/467","repository_url":"https://api.github.com/repos/nameko/nameko","labels_url":"https://api.github.com/repos/nameko/nameko/issues/467/labels{/name}","comments_url":"https://api.github.com/repos/nameko/nameko/issues/467/comments","events_url":"https://api.github.com/repos/nameko/nameko/issues/467/events","html_url":"https://github.com/nameko/nameko/issues/467","id":252424828,"node_id":"MDU6SXNzdWUyNTI0MjQ4Mjg=","number":467,"title":"Sharing Methods","user":{"login":"geoffjukes","id":1959570,"node_id":"MDQ6VXNlcjE5NTk1NzA=","avatar_url":"https://avatars2.githubusercontent.com/u/1959570?v=4","gravatar_id":"","url":"https://api.github.com/users/geoffjukes","html_url":"https://github.com/geoffjukes","followers_url":"https://api.github.com/users/geoffjukes/followers","following_url":"https://api.github.com/users/geoffjukes/following{/other_user}","gists_url":"https://api.github.com/users/geoffjukes/gists{/gist_id}","starred_url":"https://api.github.com/users/geoffjukes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geoffjukes/subscriptions","organizations_url":"https://api.github.com/users/geoffjukes/orgs","repos_url":"https://api.github.com/users/geoffjukes/repos","events_url":"https://api.github.com/users/geoffjukes/events{/privacy}","received_events_url":"https://api.github.com/users/geoffjukes/received_events","type":"User","site_admin":false},"labels":[{"id":29819327,"node_id":"MDU6TGFiZWwyOTgxOTMyNw==","url":"https://api.github.com/repos/nameko/nameko/labels/question","name":"question","color":"cc317c","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-08-23T21:40:05Z","updated_at":"2018-08-13T09:33:18Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Hi,\r\n\r\nI feel like all I do is ask questions - Sorry! I do try to read the docs, but I come from an Ops background, so some obvious things are a mystery to me - so I figure I should just ask :)\r\n\r\nIs it considered bad practice to call a method defined in an RPC service, directly?\r\nHow 'normal' is a class that defines an RPC task?\r\n\r\nFor example, what if *all* the RPC methods have the same args - and they all do the same things, like split out a filename, an extension, etc? Normally, I'd put that in an `__init__`.\r\n\r\nA contrived example, would be a File Information method that can called via RPC when the file exists in shared storage, but is called 'directly' for a file in local storage. Each of the File information calls (such as MD5) can also be called via RPC (when the caller requires only that piece of information)\r\n\r\n```\r\nimport os\r\nimport hashlib\r\nimport tempfile\r\nimport shutil\r\nfrom nameko import rpc\r\n\r\nclass FileInfoService:\r\n    name = 'file_info'\r\n\r\n    @rpc\r\n    def summary(self, filepath):\r\n        return  {'filepath': filepath,\r\n                'filename': os.path.basename(filepath),\r\n                'location': os.path.dirname(filepath),\r\n                'size': self.size(filepath),\r\n                'md5': self.md5(filepath)}\r\n\r\n    @rpc\r\n    def size(self, filepath):\r\n        return os.path.getsize(filepath)\r\n\r\n    @rpc\r\n    def md5(self, filepath):\r\n        # Calculates the MD5 of a file at `filepath`\r\n        f = open(filepath, 'rb')\r\n        m = hashlib.md5()\r\n        while True:\r\n            data = f.read(10240)\r\n            if len(data) == 0:\r\n                break\r\n            m.update(data)\r\n        return m.hexdigest()\r\n\r\n\r\nclass FileProcess:\r\n    name = 'file_process'\r\n\r\n    def __init__(self, filepath):\r\n        self.filepath = filepath\r\n        self.filename = os.path.basename(filepath)\r\n\r\n\r\n    @rpc\r\n    def process(self, filepath):\r\n        with tempfile.TemporaryDirectory(dir='/dev/shm') as tmpdir:\r\n            tmp = os.path.join(tmpdir, self.filename)\r\n            shutil.copy(self.filepath, tmp)\r\n            return self.do_stuff()\r\n\r\n    def do_stuff(self):\r\n        # Do stuff to the file that changes it\r\n        # The file only exists in local Temp storage, so I can't make an RPC call for the MD5\r\n        return FileInfoService().summary(self.filepath)\r\n```\r\n\r\nMany thanks,\r\n\r\nGeoff"},"comment":{"url":"https://api.github.com/repos/nameko/nameko/issues/comments/412461542","html_url":"https://github.com/nameko/nameko/issues/467#issuecomment-412461542","issue_url":"https://api.github.com/repos/nameko/nameko/issues/467","id":412461542,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMjQ2MTU0Mg==","user":{"login":"mattbennett","id":107611,"node_id":"MDQ6VXNlcjEwNzYxMQ==","avatar_url":"https://avatars3.githubusercontent.com/u/107611?v=4","gravatar_id":"","url":"https://api.github.com/users/mattbennett","html_url":"https://github.com/mattbennett","followers_url":"https://api.github.com/users/mattbennett/followers","following_url":"https://api.github.com/users/mattbennett/following{/other_user}","gists_url":"https://api.github.com/users/mattbennett/gists{/gist_id}","starred_url":"https://api.github.com/users/mattbennett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattbennett/subscriptions","organizations_url":"https://api.github.com/users/mattbennett/orgs","repos_url":"https://api.github.com/users/mattbennett/repos","events_url":"https://api.github.com/users/mattbennett/events{/privacy}","received_events_url":"https://api.github.com/users/mattbennett/received_events","type":"User","site_admin":false},"created_at":"2018-08-13T09:33:18Z","updated_at":"2018-08-13T09:33:18Z","author_association":"CONTRIBUTOR","body":"This issue was moved to https://discourse.nameko.io/t/sharing-methods/308"}}
{"actor":{"display_login":"mattbennett"}}