传递sqlite3命令从主进程到子进程:是否可能?

huangapple go评论54阅读模式
英文:

Passing the sqlite3 command from the main process to a child process: is it possible?

问题

我正在用Tcl进行实验,看看子进程是否可以在不阻塞主进程的情况下执行。

我想将dbws命令和通道chan传递给子进程,并让它检索数据并将其写入通道,而不会阻塞主进程。我意识到可以使用-command选项在后台执行此操作,但出于其他原因,我想知道是否可能。

请问是否可以将sqlite3命令传递给子进程,而不必在同一数据库上打开新连接?我无法使子进程识别sqlite命令。

谢谢。

英文:

I'm experimenting with Tcl to see if a child process can execute without blocking the main process.

I want to pass the dbws command and the channel chan to the child process and have it retrieve the data and write it to the channel without blocking the main process. I realize that there is a -command option that can be used to do this in the background, but I'd like to know if this is possible for other reasons.

Would you please tell me if it is possible to pass a sqlite3 command to a child process rather than having to open a new connection to the same database? I have not been able to get the child process to recognize the sqlite command.

Thank you.

package require sqlite3
if { [catch {sqlite3 dbws -create $db_name false -readonly false} result] } {
  chan puts stdout "result: $result"
  exit
}

puts stdout "Return value of exectest.tcl is:\
 [exec [info nameofexecutable] exectest.tcl dbws $chan]"


答案1

得分: 0

答案是

SQLite库需要数据库的真实文件名才能访问诸如回滚日志之类的内容;它不仅仅是将该名称未经解释地传递给open()系统调用。即使有些语言这样做,连接最好也不要在线程之间共享;这不是一个好的计划。 (特别是在单个连接中同时从两个线程启动事务是非常糟糕的。)

SQLite对Tcl的接口将每个连接严格绑定到解释器(这是所有命令的真实情况),这意味着绑定到单个进程的单个线程。它根本不公开任何底层文件描述符,但即使它这样做了,使用它们也是一个极其糟糕的主意。让每个需要数据库连接的解释器打开它自己的连接...或将工作委托给单个线程(对于写入密集型工作负载最好这样做,并根据应用程序用例可能调整事务工作方式)。

英文:

The answer is a flat No.

The SQLite library requires the real filename of the database in order to access things like the rollback journal; it doesn't just pass that name to he open() syscall uninterpreted. It is also good practice for connections to not be shared even between threads; some languages do that, but it isn't a good plan. (In particular, starting a transaction from two threads at once in a single connection is Very Bad.)

The Tcl interface to SQLite keeps each connection strictly interpreter-bound (a fact true of all commands), which implies being bound to a single thread of a single process. It does not expose any underlying file descriptors at all, but even if it did, it would be an extremely bad idea to use them. Let each interpreter that needs a database connection open it's own... or delegate the work to a single thread (write heavy workloads are best doing this, and possibly tuning the way transactions work depending on the application use case).

huangapple
  • 本文由 发表于 2023年5月13日 09:26:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定