{"id":17498369,"url":"https://github.com/pothos/awaitchannel","last_synced_at":"2025-04-14T14:29:00.410Z","repository":{"id":142163434,"uuid":"51858250","full_name":"pothos/awaitchannel","owner":"pothos","description":"Go-style concurrency and channels with Python 3.5 and asyncio","archived":false,"fork":false,"pushed_at":"2017-07-01T17:36:22.000Z","size":14,"stargazers_count":23,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-04T21:36:23.209Z","etag":null,"topics":["async-await","concurrency","go-style","python-channels"],"latest_commit_sha":null,"homepage":null,"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/pothos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-16T18:27:45.000Z","updated_at":"2024-01-19T08:08:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"004154ad-c1cc-413f-b26b-af8a6a489caa","html_url":"https://github.com/pothos/awaitchannel","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/pothos%2Fawaitchannel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pothos%2Fawaitchannel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pothos%2Fawaitchannel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pothos%2Fawaitchannel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pothos","download_url":"https://codeload.github.com/pothos/awaitchannel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248896569,"owners_count":21179456,"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":["async-await","concurrency","go-style","python-channels"],"created_at":"2024-10-19T16:50:48.450Z","updated_at":"2025-04-14T14:29:00.391Z","avatar_url":"https://github.com/pothos.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# awaitchannel\nGo-style concurrency and channels with Python 3.5 and asyncio\n\nExtends the synchronisation objects of asyncio (e.g. Lock, Event, Condition, Semaphore, Queue) with Channels like in Go.\nChannels can be used for asynchronous or synchronous message exchange.\nselect() can be used to react on finished await-calls and thus also on sending or receiving with channels.\nThe helper go() provides a simple way to schedule the concurrent functions in an event loop of a different thread.\n\n    from awaitchannel import Chan, select, go, ChannelClosed, loop\n    \n    c = Chan()  # synchronous communication\n    \n    async def give(v):\n      for i in range(0, 10):\n        await c.send(v + str(i))\n    \n    async def consume(ID):\n      async for v in c:\n        print(ID, 'got', v)\n    \n    go(consume, 'A')\n    go(consume, 'B')\n    r1 = go(give, 'x')\n    go(consume, 'C')\n    r2 = go(give, 'y')\n    # …\n    r1.result()  # wait for sending to be complete\n    r2.result()\n    go(c.close)  # cleanup pending consume iters of 'async for' (i.e. await c.recv)\n\nChan(size=0): Go-style channel with await send/recv, can also be used as an iterator which is calling recv() until a ChannelClosed exception occurs,\nsize 0 indicates a synchronous channel (handshake), size -1 indicates an unlimited buffer size, otherwise send will block when buffer size is reached\n\n@asyncio.coroutine\nchan.close() closes the channel which leads to a failure at the recv side if empty and disallows further sending,\n\n@asyncio.coroutine\nchan.send(item) async-blocks if size=0 until there is a recv and this send operation was chosen, blocks if send was used \u003csize\u003e times without a recv, blocks never for size=-1,\n\nchan.send_ready() and .recv_ready() are non-blocking testers,\n\n@asyncio.coroutine\nchan.recv() async-blocks until something is available and fails if channel is closed after all is processed\n\n\n\n@asyncio.coroutine\nselect(futures_list): parameter: select on a list of identifier-await-tuples like ['r', c.recv()), (c, c.send(2))]\n\nReturns a tuple consiting of an identifier-result-tuple like ('r', 7) or (c, None) and a special list object of pending tasks which can be directly used for the next select call or even expanded/appended on before.\nBe aware that the results are internally buffered when more complete at the same time and thus the logical ordering can be different.\n\n\ng(f, *args, **kwargs): schedule an async function on the asyncio event loop of the worker thread\n\nReturns a concurrent.future which has a (non-await, but normal) blocking .result() method to wait until the result of f() is returned.\n\n\nTo run a blocking function in a background thread and get an awaitable future for it, use the run_in_executor method of the loop:\n\n    f = loop.run_in_executor(None, normal_longrunning_function)\n    res = await f\n\nNote that you need to pass the .loop attribute of this module when you are using functions provided by asyncio yourself.\n\n\nRun an example:\n\n    PYTHONPATH=. examples/sieve.py\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpothos%2Fawaitchannel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpothos%2Fawaitchannel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpothos%2Fawaitchannel/lists"}