Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中线程化sqlite连接_Ruby_Multithreading_Sqlite_Parallel Processing - Fatal编程技术网

在Ruby中线程化sqlite连接

在Ruby中线程化sqlite连接,ruby,multithreading,sqlite,parallel-processing,Ruby,Multithreading,Sqlite,Parallel Processing,从昨天开始,我一直在尝试让我的ruby脚本线程化。此后,我选择使用SQLite来保存数据,使用并行gem来管理并发性 我已经为测试构建了一个快速脚本,但是我很难让线程正常工作;数据库已锁定。我在末尾添加了db.close,这没有用,我尝试添加sleep直到db.closed?,但这只是无限期地休眠。我做错了什么 错误为“数据库已锁定(SQLite3::BusyException)” 这是我的密码: require 'sqlite3' require 'pry' require 'parallel

从昨天开始,我一直在尝试让我的ruby脚本线程化。此后,我选择使用SQLite来保存数据,使用并行gem来管理并发性

我已经为测试构建了一个快速脚本,但是我很难让线程正常工作;数据库已锁定。我在末尾添加了db.close,这没有用,我尝试添加sleep直到db.closed?,但这只是无限期地休眠。我做错了什么

错误为“数据库已锁定(SQLite3::BusyException)”

这是我的密码:

require 'sqlite3'
require 'pry'
require 'parallel'
STDOUT.sync = true
db = SQLite3::Database.new "test.db"
arr = [1,2,3,4,5,6,7,8,9,10]

rows = db.execute <<-SQL
create table test_table (
    original string,
    conversion string
);
SQL

def test(num)
    db = SQLite3::Database.new "test.db"
    puts "the num: #{num}"
    sleep 4
    { num => num + 10}.each do |pair|
        db.execute "insert into test_table values (?, ?)", pair
    end
    db.close
end

Parallel.each( -> { arr.pop || Parallel::Stop}, in_processes: 3) { |number| test(number) }
需要'sqlite3'
需要“窥探”
要求“平行”
STDOUT.sync=true
db=SQLite3::Database.new“test.db”
arr=[1,2,3,4,5,6,7,8,9,10]
rows=db.execute{arr.pop | Parallel::Stop},in|u进程:3){number | test(number)}

默认情况下,SQLite是线程安全的(使用其“序列化”模式),ruby包装器显然在需要的任何程度上都支持这一点。但是,跨进程是不安全的,这是有一定意义的,因为适配器或引擎可能必须协商进程中的某些状态以防止锁定


要修复示例,请将\u进程中的
更改为\u线程中的

SQLLite不支持Concurent写入。您应该限制它们,或者使用另一个SQL引擎