Queue sync between Python and Laravel using Redis driver. You can process jobs dispatched from Laravel in Python.
NOTE: This package is in beta and only Redis is supported currently. Production usage is not recommended until stable release.
In the 1.0.0 stable version these implementations are planned:
- Auto-discovery jobs in both Laravel and Python.
- Auto-configuration by reading Laravel config in Python side.
- More clean API.
- Supporting more queue drivers.
pip install python-laravel-queue
- Listen for jobs on Python:
frompython_laravel_queueimportQueueasPlQueuefromredisimportRedisr=Redis(host='localhost',port=6379,db=0)queue_python=PlQueue(r,queue='python')@queue_python.handlerdefhandle(data):name=data['name']# job namedata=data['data']# job dataprint('TEST: '+data['a']+' '+data['b']+' '+data['c'])queue_python.listen()- Sending jobs from Laravel :
<?php$job =new \App\Jobs\TestJob('hi','send to','python');dispatch($job)->onQueue('python');- Schedule a job to be run in Laravel from Python:
frompython_laravel_queueimportQueueasPlQueuefromredisimportRedisr=Redis(host='localhost',port=6379,db=0)queue_laravel=PlQueue(r,queue='laravel')queue_laravel.push('App\\Jobs\\TestJob', {'a':'hello','b':'send to','c':'laravel'})TestJob in Laravel:
<?phpnamespaceApp\Jobs;useIlluminate\Contracts\Queue\ShouldQueue;useIlluminate\Bus\Queueable;useIlluminate\Contracts\Queue\ShouldQueue;useIlluminate\Foundation\Bus\Dispatchable;useIlluminate\Queue\InteractsWithQueue;useIlluminate\Queue\SerializesModels;useIlluminate\Support\Facades\Log;class TestJobextends Jobimplements ShouldQueue{use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;public$a,$b,$c;/** * Create a new job instance. * * @return void */publicfunction__construct ($a,$b,$c) {$this->a =$a;$this->b =$b;$this->c =$c; }publicfunctionhandle () { Log::info('TEST:' .$this->a .''.$this->b .'' .$this->c); }}- You need to :listen (or :work) the preferred queue name above to handle data sent from Python in Laravel.
php artisan queue:listen --queue=laravel