GithubHelp home page GithubHelp logo

> @he426100 多个协程同时用model去查询 或者插入数据就会报错 提醒不同协程不能用同一个连接了。你是怎么解决的呢?用扩展里面的Db类吗。 因为我很多东西在model里面要完成。。。所以不能用Db类的连接池 about think-swoole HOT 3 CLOSED

stonegithubs avatar stonegithubs commented on August 17, 2024
> @he426100 多个协程同时用model去查询 或者插入数据就会报错 提醒不同协程不能用同一个连接了。你是怎么解决的呢?用扩展里面的Db类吗。 因为我很多东西在model里面要完成。。。所以不能用Db类的连接池

from think-swoole.

Comments (3)

he426100 avatar he426100 commented on August 17, 2024

给个完整的代码,有空我试试

from think-swoole.

stonegithubs avatar stonegithubs commented on August 17, 2024

给个完整的代码,有空我试试
在协程代码中只有Db更新操作

<?php

declare(strict_types=1);

namespace app\command;

use think\facade\Db;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\swoole\command\Swoole;
use Swoole\Coroutine\WaitGroup;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\go;

class Test extends Swoole
{
    protected function configure()
    {
        // 指令配置
        $this->setName('swoole:test')
            ->addOption(
                'env',
                'E',
                Option::VALUE_REQUIRED,
                'Environment name',
                ''
            )
            ->setDescription('the swoole test command');
    }

    protected function runInSwoole(Input $input, Output $output)
    {
        $wg = new WaitGroup();
        // 用channel控制并发数量,config/swoole里面配置'max_active' => 64,
        $chan = new Channel(64);
        for ($c = 10000; $c--;) {
            $wg->add();
            $chan->push(true);
            go(function () use ($wg, $chan) {
                Db::transaction(function () use ($wg, $chan) {
                    try {
                        Db::name('product')->where('id',1)->update(['name' => 'abc']);
                    } catch (\Throwable $e) {
                        echo 'error: ' . $e->getMessage(), PHP_EOL;
                    } finally {
                        $chan->pop();
                        $wg->done();
                    }
                });
            });
        }
        $wg->wait();
        echo 'ok', PHP_EOL;
    }
}

from think-swoole.

stonegithubs avatar stonegithubs commented on August 17, 2024

给个完整的代码,有空我试试

非常感谢你提供的办法,之前批量操作数据,都是一条一条更新,用上你给的代码可以多个协程,速度快多了

from think-swoole.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.