Created
May 25, 2018 09:52
-
-
Save umaxfun/f2a6bbf331d3da919196a4c6c83dd2a7 to your computer and use it in GitHub Desktop.
Use Telethon inside Celery worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from celery import Celery | |
| from celery.signals import worker_process_init | |
| from telethon import TelegramClient | |
| app = Celery('tasks', broker='redis://localhost:6379/2', backend='redis://localhost:6379/3') | |
| api_id = 123 # set your api id | |
| api_hash = 'qwe' # set your api hash | |
| client = None | |
| # init one client per worker process | |
| @worker_process_init.connect | |
| def setup_connections(**kwargs): | |
| global client | |
| client = TelegramClient('session_name', api_id, api_hash) | |
| client.start() | |
| @app.task | |
| def test(): | |
| global client | |
| return client.get_me().id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment