我创建了一个 dash 应用程序来显示另一个代码正在收集的信息,我想使用 Python 中的 asyncio 模块同时运行它们。
我的代码使用异步函数,而 Dash 应用程序(基于 Flask)在服务时阻止其他任何内容执行。
我不确定这是否需要打开更多线程。
这是我当前的代码,仅运行主协程。
async def main():
some code here...
while True:
try:
await client.handle_message()
except ConnectionClosedError as error:
logger.error(error)
for strategy in strategies:
await asyncio.create_task(...)
some code here...
async def run_dashboard():
app = create_app()
app.run_server('0.0.0.0', 5000, debug=False)
if __name__ == '__main__':
some code here...
# Currently just runs the main coroutine
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)
如何同时运行 main 和 run_dashboard?
我正在使用 Ktor 客户端,并尝试使用框架提供的序列化来实现发布请求。
由于某些奇怪的原因,添加序列化可以在服务器端工作,但不能在客户端工作。
摇篮
plugins {
application
kotlin("jvm") version "1.6.10"
id("org.jetbrains.kotlin.plugin.serialization") version "1.6.10"
}
repositories {
mavenCentral()
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
}
dependencies {
// Ktor server
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
// Ktor client
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-json:$ktor_version")
implementation("io.ktor:ktor-client-serialization:$ktor_version")
// Arrow
implementation("io.arrow-kt:arrow-core:1.0.1")
// Tests
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
Run Code Online (Sandbox Code Playgroud)
在我的客户端
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.*
@Serializable
data class TastyCredentials(val login: String, val password: String) …Run Code Online (Sandbox Code Playgroud)