{"id":13398285,"url":"https://github.com/madisonmay/Tomorrow","last_synced_at":"2025-03-14T02:31:15.656Z","repository":{"id":35549607,"uuid":"39821165","full_name":"madisonmay/Tomorrow","owner":"madisonmay","description":"Magic decorator syntax for asynchronous code in Python","archived":false,"fork":false,"pushed_at":"2020-02-16T18:30:59.000Z","size":28,"stargazers_count":1459,"open_issues_count":5,"forks_count":99,"subscribers_count":49,"default_branch":"master","last_synced_at":"2025-03-10T03:06:42.917Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/madisonmay.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-28T08:06:41.000Z","updated_at":"2025-03-09T14:16:58.000Z","dependencies_parsed_at":"2022-08-19T22:10:30.312Z","dependency_job_id":null,"html_url":"https://github.com/madisonmay/Tomorrow","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madisonmay%2FTomorrow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madisonmay%2FTomorrow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madisonmay%2FTomorrow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madisonmay%2FTomorrow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/madisonmay","download_url":"https://codeload.github.com/madisonmay/Tomorrow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243511659,"owners_count":20302593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T19:00:21.807Z","updated_at":"2025-03-14T02:31:15.209Z","avatar_url":"https://github.com/madisonmay.png","language":"Python","readme":"[ ![Codeship Status for madisonmay/Tomorrow](https://codeship.com/projects/9a3b4c60-1b5b-0133-5ec7-7e346f2e432c/status?branch=master)](https://codeship.com/projects/94472)\n\n# Tomorrow\n**Magic decorator syntax for asynchronous code in Python 2.7.**\n\n\n**Please don't actually use this in production.  It's more of a thought experiment than anything else, and relies heavily on behavior specific to Python's old style classes.  Pull requests, issues, comments and suggestions welcome.**\n\nInstallation\n------------\n\nTomorrow is conveniently available via pip:\n```\npip install tomorrow\n```\n\nor installable via `git clone` and `setup.py`\n```\ngit clone git@github.com:madisonmay/Tomorrow.git\nsudo python setup.py install\n```\n\nTo ensure Tomorrow is properly installed, you can run the unittest suite from the project root:\n```\nnosetests -v \n```\n\nUsage\n-----\nThe tomorrow library enables you to utilize the benefits of multi-threading with minimal concern about the implementation details.\n\nBehind the scenes, the library is a thin wrapper around the `Future` object in `concurrent.futures` that resolves the `Future` whenever you try to access any of its attributes.\n\nEnough of the implementation details, let's take a look at how simple it is to speed up an inefficient chunk of blocking code with minimal effort.\n\n\nNaive Web Scraper\n-----------------\nYou've collected a list of urls and are looking to download the HTML of the lot.  The following is a perfectly reasonable first stab at solving the task.\n\nFor the following examples, we'll be using the top sites from the Alexa rankings.\n\n```python\nurls = [\n    'http://google.com',\n    'http://facebook.com',\n    'http://youtube.com',\n    'http://baidu.com',\n    'http://yahoo.com',\n]\n```\n\nRight then, let's get on to the code.\n\n```python\nimport time\nimport requests\n\ndef download(url):\n    return requests.get(url)\n\nif __name__ == \"__main__\":\n\n    start = time.time()\n    responses = [download(url) for url in urls]\n    html = [response.text for response in responses]\n    end = time.time()\n    print \"Time: %f seconds\" % (end - start)\n```\n\nMore Efficient Web Scraper\n--------------------------\n\nUsing tomorrow's decorator syntax, we can define a function that executes in multiple threads.  Individual calls to `download` are non-blocking, but we can largely ignore this fact and write code identically to how we would in a synchronous paradigm. \n\n```python\nimport time\nimport requests\n\nfrom tomorrow import threads\n\n@threads(5)\ndef download(url):\n    return requests.get(url)\n\nif __name__ == \"__main__\":\n    start = time.time()\n    responses = [download(url) for url in urls]\n    html = [response.text for response in responses]\n    end = time.time()\n    print \"Time: %f seconds\" % (end - start)\n\n```\n\nAwesome!  With a single line of additional code (and no explicit threading logic) we can now download websites ~10x as efficiently.\n\nYou can also optionally pass in a timeout argument, to prevent hanging on a task that is not guaranteed to return.\n\n```python\nimport time\n\nfrom tomorrow import threads\n\n@threads(1, timeout=0.1)\ndef raises_timeout_error():\n    time.sleep(1)\n\nif __name__ == \"__main__\":\n    print raises_timeout_error()\n```\n\nHow Does it Work?\n-----------------\n\nFeel free to read the source for a peek behind the scenes -- it's less than 50 lines of code.\n","funding_links":[],"categories":["Concurrency and Parallelism","Python","资源列表","Python decorator in the wild","Awesome Python"],"sub_categories":["并发和并行","Concurrency and Parallelism"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadisonmay%2FTomorrow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmadisonmay%2FTomorrow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadisonmay%2FTomorrow/lists"}