1

I want to get a value from web3.eth.getTransactionCount. It just hangs. This function works fine elsewhere(normal app, console).

To recreate this behavior simply create a new folder, add these 3 files to the folder, and inside that folder run docker-compose up. *Note that infura credentials are safe to use.

dockerfile

FROM python:3.7
WORKDIR /usr/src/app
RUN pip install flask celery[redis] web3

docker-compose.yml

version: "3"
services:
  redis:
    image: redis:5.0.7
    container_name: redis
    ports:
      - "6379:6379"
  myapp:
    build: .
    container_name: myapp
    ports:
      - "5000:5000"
    volumes:
      - .:/usr/src/app
    environment:
      - FLASK_ENV=development
      - WEB3_INFURA_PROJECT_ID=1cc71ab02b99475b8a3172b6a790c2f8
      - WEB3_INFURA_API_SECRET=6a343124ed8e4a6f9b36d28c50ad65ca
    entrypoint: |
      bash -c "python /usr/src/app/app.py"
  celery:
    build: .
    container_name: celery
    volumes:
      - .:/usr/src/app
    environment:
      - WEB3_INFURA_PROJECT_ID=1cc71ab02b99475b8a3172b6a790c2f8
      - WEB3_INFURA_API_SECRET=6a343124ed8e4a6f9b36d28c50ad65ca
    command: celery worker -A app.client -l info

app.py

from flask import Flask
from web3.auto.infura.rinkeby import w3 as web3
from celery import Celery

app = Flask(__name__)
client = Celery(app.name, broker='redis://redis:6379', backend='redis://redis:6379')

@client.task
def never_return():
    print('start')  # this is printed
    nonce = web3.eth.getTransactionCount('0x51cDD4A883144F01Bf0753b6189f3A034866465f')
    print('nonce', nonce)  # this is never printed

@app.route('/')
def index():
    never_return.apply_async()
    return "hello celery"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

I found only 1 similar unresolved post here: Call to Google Cloud API in Celery task never returns

There seems to be something weird when making a request call by other library within Celery task. Everything works fine when I tried making post requests using request. Unfortunately I don't know how to work around this problem using this request library.

Any kind of suggestions are highly appreciated.

3
  • Is it recognized as celery task when you start celery? How do you call it? Are you using celery beat and schedule? Does it run in docker container?
    – marke
    Commented Dec 7, 2019 at 11:41
  • Yes, celery console recognize the method and have successfully logged "start". Not using celery beat and schedule. Yes there are 3 containers: app, redis and celery.
    – flamengo
    Commented Dec 7, 2019 at 14:08
  • You do not call print() inside Celery tasks... Use the Celery logger instead.
    – DejanLekic
    Commented Dec 7, 2019 at 17:06

1 Answer 1

1

it seems to me this issue has something to do with websockets. so i tried to switch it to HTTP. and it works.

here is modified app.py

from flask import Flask
from web3 import Web3
from celery import Celery
from web3.middleware import geth_poa_middleware
import os

app = Flask(__name__)
client = Celery(app.name, broker='redis://redis:6379', backend='redis://redis:6379')

@client.task
def never_return():
    w3 = Web3(Web3.HTTPProvider(f"https://rinkeby.infura.io/v3/{os.getenv('WEB3_INFURA_PROJECT_ID')}", request_kwargs={'timeout': 60}))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)
    print('started')
    l = w3.eth.getBlock('latest')
    print(f'block number: {l}')
    print('finished ok')

@app.route('/')
def index():
    never_return.apply_async()
    return f"hello celery"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.