PyMySql + SQLAlchemy on Flask: create_all fails - flask

I am starting a brand new project that involves Flask + SQLAlchemy with pymysql. Currently, creation of tables based on model fails with the following:
The lines in my code (main.py) leading up to the error are:
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://myapp:myapp#localhost/myapp'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
guid = db.Column(db.String(255), unique=True)
def __init__(self):
self.guid = str(uuid.uuid1())
db.create_all()
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.
#app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello World!'
#app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, nothing at this URL.', 404
Traceback (most recent call last):
File "/Users/myuserid/Documents/Development/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/myuserid/Documents/Development/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Users/myuserid/Documents/Development/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/myuserid/Documents/Development/myapp-server/main.py", line 9, in <module>
db.create_all()
File "/Users/myuserid/Documents/Development/myapp-server/lib/flask_sqlalchemy/__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/Users/myuserid/Documents/Development/myapp-server/lib/flask_sqlalchemy/__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/sql/schema.py", line 3614, in create_all
tables=tables)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/engine/base.py", line 1850, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/engine/base.py", line 1843, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/engine/base.py", line 2034, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/engine/base.py", line 2069, in _wrap_pool_connect
return fn()
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 376, in connect
return _ConnectionFairy._checkout(self)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 708, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 480, in checkout
rec = pool._do_get()
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 1049, in _do_get
self._dec_overflow()
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 1046, in _do_get
return self._create_connection()
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 323, in _create_connection
return _ConnectionRecord(self)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 449, in __init__
self.connection = self.__connect()
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/pool.py", line 602, in __connect
connection = self.__pool._invoke_creator(self)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/engine/strategies.py", line 97, in connect
return dialect.connect(*cargs, **cparams)
File "/Users/myuserid/Documents/Development/myapp-server/lib/sqlalchemy/engine/default.py", line 377, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/connections.py", line 644, in __init__
self._connect()
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/connections.py", line 837, in _connect
self._get_server_information()
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/connections.py", line 1048, in _get_server_information
packet = self._read_packet()
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/connections.py", line 882, in _read_packet
packet_header = self._read_bytes(4)
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/connections.py", line 899, in _read_bytes
data = self._rfile.read(num_bytes)
File "/Users/myuserid/Documents/Development/myapp-server/lib/pymysql/_socketio.py", line 64, in readinto
n = e.args[0]
IndexError: tuple index out of range
INFO 2015-05-05 23:30:40,803 module.py:788] default: "GET / HTTP/1.1" 500 -

Related

Problems with starting Telegram bot

I'm trying to deploy my telegram-bot on PythonAnywhere. It has worked fine with the free account but there are some troubles with the paid one. I'm getting "OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_record', 'decryption failed or bad record mac')]" messages in Error logs (accordingly bot works wrong). How can I solve it?
I'm using python3.7 + pyTelegramBotAPI + flask
Code example for error reproducing:
# coding=utf-8
import telebot
import flask
import time
token = 'bot_token'
bot = telebot.TeleBot(token, threaded=False)
WEBHOOK_HOST = '*userName*.pythonanywhere.com'
WEBHOOK_URL_BASE = "https://%s" % (WEBHOOK_HOST)
WEBHOOK_URL_PATH = "/%s/" % (token)
app = flask.Flask(__name__)
# Process webhook calls
#app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
flask.abort(403)
#bot.message_handler(commands=['start', 'help'])
def handle_start_help(message):
bot.send_message(message.chat.id, text="Hello, my friend")
bot.remove_webhook()
time.sleep(0.1)
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)
Full error trace:
2018-10-30 12:40:33,351: Exception on /*token*/ [POST]
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/kleratoni/bot/bot/Main.py", line 90, in webhook
bot.process_new_updates([update])
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 326, in process_new_updates
self.process_new_callback_query(new_callback_querys)
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 354, in process_new_callback_query
self._notify_command_handlers(self.callback_query_handlers, new_callback_querys)
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 1490, in _notify_command_handlers
self._exec_task(message_handler['function'], message)
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 464, in _exec_task
task(*args, **kwargs)
File "/home/*accountName*/bot/bot/Main.py", line 535, in callback_inline
MainMenuActions.BetMenu_BackButton(call.message, bot, db)
File "/home/*accountName*/bot/bot/MainMenuActions.py", line 130, in BetMenu_BackButton
bot.send_message(message.chat.id, text=config.StringContent["StartPlayWords"], reply_markup=keyborads.keyboardMainmenu)
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 598, in send_message
reply_markup, parse_mode, disable_notification))
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/apihelper.py", line 140, in send_message
return _make_request(token, method_url, params=payload, method='post')
File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/apihelper.py", line 54, in _make_request
timeout=(connect_timeout, read_timeout), proxies=proxy)
File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3.7/site-packages/requests/adapters.py", line 445, in send
timeout=timeout
File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.7/http/client.py", line 1321, in getresponse
response.begin()
File "/usr/lib/python3.7/http/client.py", line 296, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.7/http/client.py", line 257, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 300, in recv_into
return self.recv_into(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 285, in recv_into
return self.connection.recv_into(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1814, in recv_into
self._raise_ssl_error(self._ssl, result)
File "/usr/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1639, in _raise_ssl_error
_raise_current_error()
File "/usr/lib/python3.7/site-packages/OpenSSL/_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_record', 'decryption failed or bad record mac')]
From an issue on the pyTelegramBotAPI github repo, it looks like it's an incompatibility between the version of requests you're using and the version of OpenSSL you're using. Try using requests 2.10.0 instead.
I had a same error and I fixed like this
token = 'bot_token'
bot = telebot.TeleBot(token, threaded=False)

Why does Celery give No such file or directory error?

I generate a PDF and send it through email as attachment. Whenever I run celery multi start worker -A erp -l info and try to send the email it works. But whenever I do it with the daemonized Celery it shows the following error.
[2018-05-11 20:53:47,131: ERROR/ForkPoolWorker-6] Task europarts.tasks.generate_pdf_and_send_email[fd80843b-647c-412e-b847-f35cbf157fae] raised unexpected: OSError(2, 'No such file or directory')
Traceback (most recent call last):
File "/home/michel/erp/venv/lib/python2.7/site-packages/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/michel/erp/venv/lib/python2.7/site-packages/celery/app/trace.py", line 629, in __protected_call__
return self.run(*args, **kwargs)
File "europarts/tasks.py", line 58, in generate_pdf_and_send_email
email.attach(file_name, response.rendered_content, 'application/pdf')
File "/home/michel/erp/venv/lib/python2.7/site-packages/wkhtmltopdf/views.py", line 78, in rendered_content
cmd_options=cmd_options
File "/home/michel/erp/venv/lib/python2.7/site-packages/wkhtmltopdf/utils.py", line 186, in render_pdf_from_template
cmd_options=cmd_options)
File "/home/michel/erp/venv/lib/python2.7/site-packages/wkhtmltopdf/utils.py", line 124, in convert_to_pdf
return wkhtmltopdf(pages=filename, **cmd_options)
File "/home/michel/erp/venv/lib/python2.7/site-packages/wkhtmltopdf/utils.py", line 110, in wkhtmltopdf
return check_output(ck_args, **ck_kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 568, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I can see that on line 58 I have and error in my function. But that function does not write a file to disk, just creates the PDF and attaches it to email before sending. Here is the code:
def generate_pdf_and_send_email(template, filename, context, pk, model, subject, body, from_email, to):
request = RequestFactory().get(reverse('europarts:{model}_email'.format(model=model), args=[pk]))
kw = {
'{model}_id'.format(model=model): pk
}
row_model = '{model}Row'.format(model=model)
rows = apps.get_model('europarts', row_model).objects.filter(**kw)
context['{model}_rows'.format(model=model)] = rows
file_name = '{model}_email.pdf'.format(model=model)
response = PDFTemplateResponse(
request=request,
template=template,
filename=filename,
context=context,
show_content_in_browser=True,
cmd_options={'margin-top': 10,
'zoom': 1,
'viewport-size': '1366 x 513',
'javascript-delay': 1000,
'no-stop-slow-scripts': True},
)
email = EmailMessage()
email.subject = subject
email.body = body
email.from_email = from_email
email.to = to
email.attach(file_name, response.rendered_content, 'application/pdf')
email.send()
return response
What could I be doing wrong?
My problem was solved by finding out where wkhtmltopdf was installed:
$ which wkhtmltopdf
/usr/local/bin/wkhtmltopdf
And putting that location in settings.py:
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'

Error with Django Celery ,how to fix?

well i have same view same setting for celery in another app, when i launch another one, he is work, absolutely work, but when i launch this, nothing, i installed everthyng like in another one, but nothing, pls help me!
if you have some ideas how to fix please let me know)
This is full traceback
Traceback (most recent call last):
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\utils\__init__.py", line 423, in __call__
return self.__value__
AttributeError: 'ChannelPromise' object has no attribute '__value__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 449, in _ensured
return fun(*args, **kwargs)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\messaging.py", line 177, in _publish
channel = self.channel
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\messaging.py", line 194, in _get_channel
channel = self._channel = channel()
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\utils\__init__.py", line 425, in __call__
value = self.__value__ = self.__contract__()
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\messaging.py", line 209, in <lambda>
channel = ChannelPromise(lambda: connection.default_channel)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 771, in default_channel
self.connection
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 756, in connection
self._connection = self._establish_connection()
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 711, in _establish_connection
conn = self.transport.establish_connection()
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\transport\pyamqp.py", line 116, in establish_connection
conn = self.Connection(**opts)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\connection.py", line 165, in __init__
self.transport = self.Transport(host, connect_timeout, ssl)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\connection.py", line 186, in Transport
return create_transport(host, connect_timeout, ssl)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\transport.py", line 299, in create_transport
return TCPTransport(host, connect_timeout)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\transport.py", line 95, in __init__
raise socket.error(last_err)
OSError: [WinError 10061]The connection is not established, because the destination computer rejected the connection request
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\src\accounts\decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\src\accounts\views.py", line 41, in signup
register_task.delay(username, password, email, get_secret_key)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\celery\app\task.py", line 453, in delay
return self.apply_async(args, kwargs)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\celery\app\task.py", line 559, in apply_async
**dict(self._get_exec_options(), **options)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\celery\app\base.py", line 353, in send_task
reply_to=reply_to or self.oid, **options
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\celery\app\amqp.py", line 305, in publish_task
**kwargs
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\messaging.py", line 172, in publish
routing_key, mandatory, immediate, exchange, declare)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 470, in _ensured
interval_max)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 382, in ensure_connection
interval_start, interval_step, interval_max, callback)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\utils\__init__.py", line 246, in retry_over_time
return fun(*args, **kwargs)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 250, in connect
return self.connection
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 756, in connection
self._connection = self._establish_connection()
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\connection.py", line 711, in _establish_connection
conn = self.transport.establish_connection()
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\kombu\transport\pyamqp.py", line 116, in establish_connection
conn = self.Connection(**opts)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\connection.py", line 165, in __init__
self.transport = self.Transport(host, connect_timeout, ssl)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\connection.py", line 186, in Transport
return create_transport(host, connect_timeout, ssl)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\transport.py", line 299, in create_transport
return TCPTransport(host, connect_timeout)
File "C:\Users\P.A.N.D.E.M.I.C\Desktop\shop\lib\site-packages\amqp\transport.py", line 95, in __init__
raise socket.error(last_err)
OSError: [WinError 10061] `The connection is not established, because the destination computer rejected the connection request`
this is celery
from __future__ import absolute_import
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
from django.conf import settings
from celery import Celery
app = Celery('src')
# This reads, e.g., CELERY_ACCEPT_CONTENT = ['json'] from settings.py:
app.config_from_object('django.conf:settings')
# For autodiscover_tasks to work, you must define your tasks in a file called 'tasks.py'.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
#app.task(bind=True)
def debug_task(self):
print("Request: {0!r}".format(self.request))
this is settings
# CELERY STUFF
BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
this is view
#csrf_protect
#check_recaptcha
def signup(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
print(form)
if form.is_valid() and request.recaptcha_is_valid:
print(form.cleaned_data)
email = form.cleaned_data['email']
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
get_secret_key = activation_key_generator(username)
link = "127.0.0.1:8000/accounts/activate/{0}".format(get_secret_key)
html_message = '<h3>Your username: <b>{0}</b> <br> <b>Your password: <b>{1}</b></h3><br><h1>That to get full access to our services, go through link below!</h1> <br> Activate need raw link? <br> {2}'.format(username, password, link)
register_task.delay(username, password, email, get_secret_key)
send_some_email_task.delay(email, username, html_message)
return redirect('/accounts/register/verification')
form = RegistrationForm(request.POST or None)
context = {'form':form}
return render(request, 'accounts/registration/signup.html', context)
You said in the comments you are using Redis but the configuration you have is for RabbitMQ. Check Using Redis page for more info on configuration. More specifically, your broker URL should be something like redis://:password#hostname:port/db_number, not amqp.

RabbitMQ RPC call works in real life, but doesn't work in tests

I have Flask application and implemented RPC in it.
engine.py
import pickle
import pika
from databasyfacade.rpc import api
__author__ = 'Marboni'
def on_request(ch, method, props, body):
request = pickle.loads(body)
func = getattr(api, request['func'])
try:
result = func(*request['args'])
except Exception, e:
response = {
'status': 'ERROR',
'error': e
}
else:
response = {
'status': 'OK',
'result': result
}
ch.basic_publish(exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(
correlation_id=props.correlation_id
),
body=pickle.dumps(response)
)
ch.basic_ack(delivery_tag=method.delivery_tag)
def init(host):
connection = pika.BlockingConnection(pika.ConnectionParameters(host))
channel = connection.channel()
channel.queue_declare(queue='facade_rpc')
channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_request, queue='facade_rpc')
api.py
from databasyfacade.services import profiles_service
__author__ = 'Marboni'
def profile(user_id):
return profiles_service.profile(user_id)
When Flask application initializes, it runs method init(host).
Now I need to test how my application responses to RPC calls. So I wrote following RPC client:
client.py
import pickle
import uuid
import pika
__author__ = 'Marboni'
class RpcClient(object):
def __init__(self, host):
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=host))
self.channel = self.connection.channel()
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(self.on_response, no_ack=True, queue=self.callback_queue)
def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = body
def call(self, func, *args):
request = {
'func': func,
'args': args
}
self.response = None
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(exchange='',
routing_key='facade_rpc',
properties=pika.BasicProperties(
reply_to = self.callback_queue,
correlation_id = self.corr_id,
),
body=pickle.dumps(request))
while self.response is None:
self.connection.process_data_events()
response = pickle.loads(self.response)
if response['status'] == 'ERROR':
e = response['error']
raise e
else:
return response['result']
Then I wrote test based on Flask-Testing framework. It initializes Flask application between each test method, so we can interact with it.
tests.py
from databasyfacade.rpc import RpcClient
from databasyfacade.testing import DatabasyTest, fixtures
from databasyfacade.testing.testdata import UserData, ProfileData
__author__ = 'Marboni'
class RpcTest(DatabasyTest):
#fixtures(UserData, ProfileData)
def test_profile(self, data):
rpc = RpcClient(self.app.config['RABBITMQ_HOST'])
profile = rpc.call('profile', ProfileData.hero.user_id)
self.assertIsNotNone(profile)
self.assertEqual(ProfileData.hero.email, profile.email)
This test hangs when makes call. It iterates infinitely here:
From client.py
while self.response is None:
self.connection.process_data_events()
It means that on_response() method on client never called.
If I interrupt my tests with CTRL-C, I will see following stacktrace:
Traceback (most recent call last):
File "../env/bin/nosetests", line 8, in <module>
load_entry_point('nose==1.3.0', 'console_scripts', 'nosetests')()
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/core.py", line 118, in __init__
**extra_args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 95, in __init__
self.runTests()
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/core.py", line 197, in runTests
result = self.testRunner.run(self.test)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/core.py", line 61, in run
test(result)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
return self.run(*arg, **kw)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 223, in run
test(orig)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/suite.py", line 65, in __call__
return self.run(*args, **kwds)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 74, in run
test(result)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
return self.run(*arg, **kw)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 223, in run
test(orig)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
return self.run(*arg, **kw)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 223, in run
test(orig)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
return self.run(*arg, **kw)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 223, in run
test(orig)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
return self.run(*arg, **kw)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 223, in run
test(orig)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
return self.run(*arg, **kw)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/suite.py", line 223, in run
test(orig)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/case.py", line 45, in __call__
return self.run(*arg, **kwarg)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/case.py", line 133, in run
self.runTest(result)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/nose/case.py", line 151, in runTest
test(result)
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/flask_testing.py", line 72, in __call__
self._pre_setup()
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/flask_testing.py", line 80, in _pre_setup
self.app = self.create_app()
File "/Users/Marboni/Projects/Databasy/databasy-facade/databasyfacade/databasyfacade/testing/__init__.py", line 12, in create_app
return app.create_app()
File "/Users/Marboni/Projects/Databasy/databasy-facade/databasyfacade/databasyfacade/app.py", line 75, in create_app
init_rpc(app)
File "/Users/Marboni/Projects/Databasy/databasy-facade/databasyfacade/databasyfacade/app.py", line 63, in init_rpc
rpc.init(app.config['RABBITMQ_HOST'])
File "/Users/Marboni/Projects/Databasy/databasy-facade/databasyfacade/databasyfacade/rpc/engine.py", line 39, in init
channel.basic_consume(on_request, queue='facade_rpc')
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/pika/channel.py", line 220, in basic_consume
{'consumer_tag': consumer_tag})])
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1104, in _rpc
self._wait_on_response(method_frame))
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1124, in _send_method
self.connection.process_data_events()
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 215, in process_data_events
if self._handle_read():
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 327, in _handle_read
if self._read_poller.ready():
File "/Users/Marboni/Projects/Databasy/databasy-facade/env/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 66, in ready
self.poll_timeout)
KeyboardInterrupt
I tried to run application and access it from separate script:
#!/usr/bin/env python
from databasyfacade.rpc.client import RpcClient
rpc = RpcClient('localhost')
profile = rpc.call('profile', 4L)
print profile.email
As you can see, code is the same as in test, but in this case it works.
What can be the cause of this issue? May be, it's because Flask-Testing runs both application and client in one process? How to check it / write correct test?
I found the cause, it's not related with MQ. Remove method accessed database using SQLAlchemy with scoped_session. Problem disappeared after I finalized session properly:
Session.remove()

Django-Celery 2.5.3 BROKER_URL Error 'No such transport: mysql'

i upgraded my celery from 2.4.X to celery-2.5.2, then modified the Broker_URL to be
BROKER_TRANSPORT = "sqlalchemy"
BROKER_URL = "sqla+mysql://root:root#localhost:3306/db"
this config can be referred via the page of http://docs.celeryproject.org/en/latest/getting-started/brokers/sqlalchemy.html#broker-sqlalchemy
the Celery process can be started successfully, but whan i call .delay or apply_async the process will throw out exception KeyError: 'No such transport: mysql'
i have no idea about this.. looks like everything should be right.
Traceback
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\celery-2.5.2-py2.7.egg\celery\app\task\__init__.py", line 353, in delay
return self.apply_async(args, kwargs)
File "C:\Python27\lib\site-packages\celery-2.5.2-py2.7.egg\celery\app\task\__init__.py", line 449, in apply_async
publish = publisher or self.app.amqp.publisher_pool.acquire(block=True)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 625, in acquire
R = self.prepare(R)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\pools.py", line 55, in prepare
p = p()
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\pools.py", line 46, in <lambda>
return lambda: self.create_producer()
File "C:\Python27\lib\site-packages\celery-2.5.2-py2.7.egg\celery\app\amqp.py", line 265, in create_producer
pub = self.app.amqp.TaskPublisher(conn, auto_declare=False)
File "C:\Python27\lib\site-packages\celery-2.5.2-py2.7.egg\celery\app\amqp.py", line 328, in TaskPublisher
return TaskPublisher(*args, **self.app.merge(defaults, kwargs))
File "C:\Python27\lib\site-packages\celery-2.5.2-py2.7.egg\celery\app\amqp.py", line 158, in __init__
super(TaskPublisher, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\compat.py", line 61, in __init__
super(Publisher, self).__init__(connection, self.exchange, **kwargs)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\messaging.py", line 69, in __init__
channel = channel.default_channel
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 556, in default_channel
self.connection
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 549, in connection
self._connection = self._establish_connection()
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 509, in _establish_connection
conn = self.transport.establish_connection()
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 569, in transport
self._transport = self.create_transport()
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 324, in create_transport
return self.get_transport_cls()(client=self)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\connection.py", line 331, in get_transport_cls
transport_cls = get_transport_cls(transport_cls)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\transport__init__.py", line 94, in get_transport_cls
_transport_cache[transport] = _get_transport_cls(transport)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\transport\__init__.py", line 75, in _get_transport_cls
transport_module_name, transport_cls_name = resolve_transport(transport)
File "C:\Python27\lib\site-packages\kombu-2.1.5-py2.7.egg\kombu\transport\__init__.py", line 70, in resolve_transport
raise KeyError("No such transport: %s" % (transport, ))
KeyError: 'No such transport: mysql'
After some debugging I've tracked this down to the version of kombu being installed. Kombu 2.1.5 was released on April 13th and does not appear to be compatible. I was able to resolve this issue by explicitly degrading to version 2.1.3.