Monday, 7 October 2019

Flask TypeError: __init__() takes 1 positional argument but 2 were given

I get the following error from my flask application:

* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.6/concurrent/futures/process.py", line 272, in _queue_management_worker
    result_item = reader.recv()
  File "/usr/lib/python3.6/multiprocessing/connection.py", line 251, in recv
    return _ForkingPickler.loads(buf.getbuffer())
TypeError: __init__() takes 1 positional argument but 2 were given

I am sending a POST request using curl such as:

curl -H "Content-type: application/json" -X POST http://0.0.0.0:5000/ -d '{"url" : "https://www.example.org/file}'

My flask file look like :

from app import app
from flask import request, Flask, json
from pdfocr import main

@app.route('/', methods = ['POST'])
def api_message():
    if request.headers['Content-Type'] == 'application/json':
       xx =  main(request.json['url'])
    return xx
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False)

and app.py is as follows:

from flask import Flask
import os
import shutil

if not os.path.exists('./datadir'):
        os.mkdir('./datadir')
else:
        shutil.rmtree('./datadir')
        os.mkdir('./datadir')


UPLOAD_FOLDER = './datadir'

app = Flask(__name__)
app.secret_key = "secret key"
#app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 80 * 1024 * 1024

I don't understand why I'm getting an error in the threading.py file, since I'm not even using it plus I didn't think this TypeError would be encountered in a standard python module, I don't get this error on my local machine and it works perfectly, but I get this error while working on docker. Would love an explanation as to why this is happening and how to resolve this.

Here's the code to main function.

def main(pdf_path):
    """
    Executes the pdf_pags and process function by calling 4 batches
    """
    #import pdb; pdb.set_trace()
    if Find(pdf_path):
        r = requests.get(pdf_path)
        full_path = os.path.join('./datadir',pdf_path.rsplit('/', 1)[1] + '.pdf')
        with open(full_path, 'wb') as f:
            f.write(r.content)
        pdf_pages = image_path(full_path)
    else:
        abs_path = os.path.abspath(pdf_path)
        print('[v]Absolute Path', abs_path)
        new = time.time()
        pdf_pages = image_path(abs_path)
        print('IMAGE CREATION', time.time()-new)
    with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
        var = executor.map(process, pdf_pages)
    return "".join(list(var))

def Find(string):
    # findall() has been used  
    # with valid conditions for urls in string 
    url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string)
    return url


from Flask TypeError: __init__() takes 1 positional argument but 2 were given

No comments:

Post a Comment