Repository: stable-diffusion-webui
Issue: [Bug]: when i use api "/sdapi/v1/txt2img", raise error "RuntimeError: There is no current event loop in thread 'AnyIO worker thread'."
Labels: bug-report

### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

excute the api
![image](https://user-images.githubusercontent.com/886701/230579153-db87f76e-a6cc-4d23-baa7-fef3acecf110.png)

![image](https://user-images.githubusercontent.com/886701/230579196-0ecdd2ff-ecff-45d0-919c-18cdb6dd4f14.png)


### Steps to reproduce the problem

1: update to the new code
2:run with --api
3:excute api

### What should have happened?

no

### Commit where the problem happens

master

### What platforms do you use to access the UI ?

Linux

### What browsers do you use to access the UI ?

Google Chrome

### Command Line Arguments

```Shell
--api
```


### List of extensions

no

### Console logs

```Shell
API error: POST: http://182.92.108.154:7860/sdapi/v1/txt2img {'error': 'RuntimeError', 'detail': '', 'body': '', 'errors': "There is no current event loop in thread 'AnyIO worker thread'."}
Traceback (most recent call last):
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/anyio/streams/memory.py", line 94, in receive
    return self.receive_nowait()
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/anyio/streams/memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/base.py", line 78, in call_next
    message = await recv_stream.receive()
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/anyio/streams/memory.py", line 114, in receive
    raise EndOfStream
anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/stable-diffusion-webui/modules/api/api.py", line 145, in exception_handling
    return await call_next(request)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/base.py", line 108, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/data/stable-diffusion-webui/modules/api/api.py", line 110, in log_and_time
    res: Response = await call_next(req)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 24, in __call__
    await responder(scope, receive, send)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/gzip.py", line 44, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/fastapi/routing.py", line 165, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/data/stable-diffusion-webui/modules/api/api.py", line 284, in text2imgapi
    self.default_script_arg_txt2img = self.init_default_script_args(script_runner)
  File "/data/stable-diffusion-webui/modules/api/api.py", line 254, in init_default_script_args
    script_args[script.args_from:script.args_to] = ui_default_values
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/gradio/blocks.py", line 1160, in __exit__
    self.app = routes.App.create_app(self)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/gradio/routes.py", line 143, in create_app
    app = App(default_response_class=ORJSONResponse)
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/site-packages/gradio/routes.py", line 113, in __init__
    self.lock = asyncio.Lock()
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/asyncio/locks.py", line 81, in __init__
    self._loop = events.get_event_loop()
  File "/data/tools/miniconda3/envs/sd/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'AnyIO worker thread'.
```


### Additional information

_No response_