|
2261 | 2261 | { |
2262 | 2262 | "data": { |
2263 | 2263 | "text/plain": [ |
2264 | | - "['h', 'g', 'c', 'f', 'e', 'a', 'd', 'b']" |
| 2264 | + "['e', 'b', 'g', 'd', 'h', 'c', 'f', 'a']" |
2265 | 2265 | ] |
2266 | 2266 | }, |
2267 | 2267 | "execution_count": null, |
|
2657 | 2657 | { |
2658 | 2658 | "data": { |
2659 | 2659 | "text/plain": [ |
2660 | | - "'_sKl4z921TkyyYSWEcGSsbQ'" |
| 2660 | + "'_z9sXygihSNeFX55bTEVs3A'" |
2661 | 2661 | ] |
2662 | 2662 | }, |
2663 | 2663 | "execution_count": null, |
|
2752 | 2752 | { |
2753 | 2753 | "data": { |
2754 | 2754 | "text/plain": [ |
2755 | | - "('5ca7845c', '8c7d7247')" |
| 2755 | + "('40e6f9cb', '8c7d7247')" |
2756 | 2756 | ] |
2757 | 2757 | }, |
2758 | 2758 | "execution_count": null, |
|
3504 | 3504 | "name": "stdout", |
3505 | 3505 | "output_type": "stream", |
3506 | 3506 | "text": [ |
3507 | | - "Num Events: 9, Freq/sec: 340.6\n", |
3508 | | - "Most recent: ▅▃▇▁▅ 268.0 265.5 297.0 225.3 273.3\n" |
| 3507 | + "Num Events: 9, Freq/sec: 344.8\n", |
| 3508 | + "Most recent: ▅▃▆▁▇ 266.1 261.7 279.1 235.3 283.6\n" |
3509 | 3509 | ] |
3510 | 3510 | } |
3511 | 3511 | ], |
|
4642 | 4642 | "print(await r) # \"data\" (no delay)" |
4643 | 4643 | ] |
4644 | 4644 | }, |
| 4645 | + { |
| 4646 | + "cell_type": "code", |
| 4647 | + "execution_count": null, |
| 4648 | + "id": "1fc78492", |
| 4649 | + "metadata": {}, |
| 4650 | + "outputs": [], |
| 4651 | + "source": [ |
| 4652 | + "#| export\n", |
| 4653 | + "def is_async_callable(obj):\n", |
| 4654 | + " \"Check if `obj` is an async callable, handling `partial` wrappers and callable instances\"\n", |
| 4655 | + " # Implementation from Starlette; Copyright © 2018, Encode OSS Ltd.\n", |
| 4656 | + " from asyncio import iscoroutinefunction\n", |
| 4657 | + " while isinstance(obj, partial): obj = obj.func\n", |
| 4658 | + " return iscoroutinefunction(obj) or (callable(obj) and iscoroutinefunction(obj.__call__))" |
| 4659 | + ] |
| 4660 | + }, |
| 4661 | + { |
| 4662 | + "cell_type": "markdown", |
| 4663 | + "id": "c498168d", |
| 4664 | + "metadata": {}, |
| 4665 | + "source": [ |
| 4666 | + "`is_async_callable` detects whether an object can be called asynchronously. It goes beyond `asyncio.iscoroutinefunction` by also handling `functools.partial`-wrapped async functions (unwrapping through any number of layers) and callable objects whose `__call__` method is a coroutine. The implementation is thanks to [Starlette](https://github.com/encode/starlette)." |
| 4667 | + ] |
| 4668 | + }, |
| 4669 | + { |
| 4670 | + "cell_type": "code", |
| 4671 | + "execution_count": null, |
| 4672 | + "id": "0079316a", |
| 4673 | + "metadata": {}, |
| 4674 | + "outputs": [], |
| 4675 | + "source": [ |
| 4676 | + "async def f(): pass\n", |
| 4677 | + "assert is_async_callable(f)\n", |
| 4678 | + "assert is_async_callable(partial(f))\n", |
| 4679 | + "assert not is_async_callable(lambda: None)" |
| 4680 | + ] |
| 4681 | + }, |
| 4682 | + { |
| 4683 | + "cell_type": "code", |
| 4684 | + "execution_count": null, |
| 4685 | + "id": "cfb13929", |
| 4686 | + "metadata": {}, |
| 4687 | + "outputs": [], |
| 4688 | + "source": [ |
| 4689 | + "class AsyncObj:\n", |
| 4690 | + " async def __call__(self): pass\n", |
| 4691 | + "\n", |
| 4692 | + "class SyncObj:\n", |
| 4693 | + " def __call__(self): pass\n", |
| 4694 | + "\n", |
| 4695 | + "assert is_async_callable(AsyncObj())\n", |
| 4696 | + "assert not is_async_callable(SyncObj())" |
| 4697 | + ] |
| 4698 | + }, |
| 4699 | + { |
| 4700 | + "cell_type": "markdown", |
| 4701 | + "id": "81493a56", |
| 4702 | + "metadata": {}, |
| 4703 | + "source": [ |
| 4704 | + "### flexicache" |
| 4705 | + ] |
| 4706 | + }, |
4645 | 4707 | { |
4646 | 4708 | "cell_type": "code", |
4647 | 4709 | "execution_count": null, |
|
4883 | 4945 | ] |
4884 | 4946 | } |
4885 | 4947 | ], |
4886 | | - "metadata": { |
4887 | | - "kernelspec": { |
4888 | | - "display_name": "python3", |
4889 | | - "language": "python", |
4890 | | - "name": "python3" |
4891 | | - } |
4892 | | - }, |
| 4948 | + "metadata": {}, |
4893 | 4949 | "nbformat": 4, |
4894 | 4950 | "nbformat_minor": 5 |
4895 | 4951 | } |
0 commit comments