@@ -93,6 +93,7 @@ async def prompt(
9393 show_default : bool = True ,
9494 err : bool = False ,
9595 show_choices : bool = True ,
96+ blocking : bool = True ,
9697) -> t .Any :
9798 """Prompts a user for input. This is a convenience function that can
9899 be used to prompt a user for input later.
@@ -120,11 +121,18 @@ async def prompt(
120121 For example if type is a Choice of either day or week,
121122 show_choices is true and text is "Group by" then the
122123 prompt will be "Group by (day, week): ".
124+ :param blocking: if `False`, uses a thread to interact with the terminal.
125+ This keeps the event loop running but may cause interesting
126+ effects when interrupted with Control-C. The default
127+ is `True`, but don't depend on it.
123128
124129 Warning: The user interaction is run inside a separate thread, because otherwise
125130 the call would block the async loop. As a result, behavior when interrupted may be
126131 sub-optimal.
127132
133+ .. versionadded:: 8.3
134+ (AsyncClick) ``prompt`` is now async; ``blocking`` parameter added.
135+
128136 .. versionadded:: 8.0
129137 ``confirmation_prompt`` can be a custom string.
130138
@@ -156,6 +164,13 @@ def prompt_func(text: str) -> str:
156164 echo (None , err = err )
157165 raise Abort () from None
158166
167+ if blocking :
168+ async def run_prompt_func (text : str ) -> str :
169+ return prompt_func (text )
170+ else :
171+ def run_prompt_func (text : str ) -> Awaitable [str ]:
172+ return anyio .to_thread .run_sync (prompt_func , text )
173+
159174 if value_proc is None :
160175 value_proc = convert_type (type , default )
161176
@@ -171,7 +186,7 @@ def prompt_func(text: str) -> str:
171186
172187 while True :
173188 while True :
174- value = await anyio . to_thread . run_sync ( prompt_func , prompt )
189+ value = await run_prompt_func ( prompt )
175190 if value :
176191 break
177192 elif default is not None :
@@ -190,7 +205,7 @@ def prompt_func(text: str) -> str:
190205 if not confirmation_prompt :
191206 return result
192207 while True :
193- value2 = await anyio . to_thread . run_sync ( prompt_func , confirmation_prompt )
208+ value2 = await run_prompt_func ( confirmation_prompt )
194209 is_empty = not value and not value2
195210 if value2 or is_empty :
196211 break
0 commit comments