Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
[Best Practices Question] Can you help me decide if my approach to threading seems reasonable.

So I am building a server application that will have a maximum of 2000 tcp socket listener threads and I want to make sure I am approaching this with the best possible techniques.

These threads will be spawned when the tcp server detects a new connection, the thread will listen for 60 seconds and timeout repeatedly until data has been streamed to the connection or the connection has timed\-out 5 times. If data has been received, it caches the data into the thread until a full protocol command has been detected and it queues the command to be read by the main thread. If the command queue is being read by the main thread, the listener thread will get halted before adding anything to the queue until it is unlocked again. The main thread only reads the queue every 0.6 of a second.

All of the advice I have found on TCP in python teaches to spawn a new thread for the listeners, is this still recommended for a maximum of 2000 connections? Should I try to use multithreading.Pool instead or something different all together?

I thought it would help to time the connection out 5 times, every 60 seconds for 5 minutes, as opposed to setting a hard 5 minute timeout to the TCPClient. Is this a reasonable assumption or unnecessary if the only code I have in the thread is the listener?

The only object access being shared by threads is the protocol command queue which is happening every 0.6 of a second and a client can send hundreds of commands in that span of time. The command queue is hosted in the main thread and every client listener has the ability to Enqueue\(\) a command and the main thread is the only one allowed to Dequeue\(\). Is it necessary for me to program my own locking system for the queue object, or is locking necessary with queue.Queue.Enqueue? If two Enqueue\(\) methods are called at the exact same time from different threads will this cause any issue like overwriting a queued entry or will it just affect the possible order the queued entries come out?

Any help would be great, I imagine what I have currently would be fine for maybe even 100 people. But i feel like this approach is just not the best for 2000 people all trying to send commands to the queue.

/r/Python
https://redd.it/8otbl2
I finished my first python project.

I finally finished my first project in python and created a repository on [GitHub](https://github.com/HungryWalrus/Chem). Any feedback would be nice.

/r/Python
https://redd.it/8osfwi
[AF] No support for ALTER of constraints in sqlite dialect, flask-migrate.

Hi, so i have been trying to create a model called events which has a one to many relationship with the posts model, so an event can have many different posts.

Here is the part of the post model that is relevant:

event_id = db.Column(db.Integer, db.ForeignKey('event.id'))

And here is the event model:

class Event(db.Model):
id = db.Column(db.Integer, primary_key=True)
event_name = db.Column(db.String(150), index=True, unique=True)
posts = db.relationship('Post', backref='event', lazy='dynamic')

The part of the upgrade function, in the automatically generated alembic file, that is causing this problem is this:

op.create_foreign_key(None, 'post', 'event', ['event_id'], ['id'])

The error given states:

NotImplementedError: No support for ALTER of constraints in SQLite dialect.

Before making the edits the post model did have a column named event, which I've deleted now, does it have something to do with that?

[Here](https://www.github.com/ModoUnreal/nuncio) is the repo for further reference.

What is the problem here and how do I fix it?

/r/flask
https://redd.it/8ozw5e
Help with git checkout causing problem

It is the first time that I have deployed a wepapp. I needed to to run/execute `git checkout -- .` to clear all the changes I had made on the server before doing `git pull origin master`.

Accidentally, I ran `git checkout --` (notice the absence of `.`) and I received something like
`M a_file (.py file)`

`M b_file (.css)`

`M c_file (.css)`

I realised my mistake and corrected by running `git checkout -- .` followed by `git pull origin master`.

I restarted my app and saw that the app looked weird, and some of the styles were missing. Also, `.py` file mentioned above is not getting updated despite me doing `git pull origin master` multiple times.

Please provide hints/suggestions to get my app working properly.

/r/djangolearning
https://redd.it/8ozsfl
Saleor vs Oscar vs Home-made

Hello !

I've built my online shop without a dedicated e\-commerce library, just plugging into Stripe manually to deal with sales.

I'm beginning to feel the limits of this approach, especially with regards to integrating my site with third\-party services : stripe, google analytics, paypal, zapier, ...

For example, I'd like to activate 3D secure on my site, and add paypal support. It would probably be very easy to do with Saleor and Oscar, but I have to do it manually as of now. The downside to implementing these frameworks is that I've already got a working sales funnel, and I'm afraid of the amount of work required to fit the frameworks to my existing setup.

As a solo dev, do you think I should keep going with the manual approach, or do you think Saleor and Oscar would be good ways to upgrade my site ? What is your experience with them ?

/r/django
https://redd.it/8p1h97
[D] Dedicated to all those researchers in fear of being scooped :)

/r/MachineLearning
https://redd.it/8p169l
[JupyterLab] How to Run R Code AND Python Code in Same Notebook?

I am trying to run some python code, then carry those created objects through some R code, then back to python within the same notebook. I know it was possible in Jupyer Notebook as I have seen many tutorials on how to do it.
Jupyter notebook used to have things like rpy2 but that does not seem to work anymore. Does anyone know how to accomplish this in JupyterLab in anaconda?

Thanks in Advance!

/r/IPython
https://redd.it/8p4m0m
Login through api

Hello guys what would be the best way to login through a api and to keep the user logged in like flask-login do and that can offer the best protection for the api

/r/flask
https://redd.it/8p5cnn
Flask in Docker, some problems here.

Hello everyone,
once again i have a problem with a docker app. To be more precise i want to learn python flask development, so i created an docker image, here it is:
`FROM python:alpine`
`WORKDIR /app`
`COPY ./app/requirements.txt .`
`RUN pip install --no-cache-dir -U -r requirements.txt`
Pretty basic. Just installs flask into my container.
Then i have the following docker\-compose file:
`version: '3'`
`services:`
`flaskapp:`
`build: .`
`container_name: flaskapp`
`volumes:`
`- $PWD/app:/app`
`environment:`
`- FLASK_APP=app.py`
`- FLASK_SETTINGS=config/config.py`
`ports:`
`- "5000:5000"`
`command: flask run`
As you see, there are two environment vars that i want to pass to my container. First, the actual flask app that is in the /app folder. Then, the localtion of my config file which is in the /config directory.
Here is my app.py file:
`from flask import Flask`
`import os`
`app = Flask(__name__)`
`app.config.from_envvar('FLASK_SETTINGS')`
`@app.route('/')`
`def index():`
`return 'Index Page'`
Here is the config file:
`DEBUG = True`
`HOST = '0.0.0.0'`
So now to the actual misbehavior, when i start the container i get the following:
`docker-compose up`
`Starting flaskapp ... done`
`Attaching to flaskapp`
`flaskapp | * Serving Flask app "app.py"`
`flaskapp | * Environment: production`
`flaskapp | WARNING: Do not use the development server in a production environment.`
`flaskapp | Use a production WSGI server instead.`
`flaskapp | * Debug mode: off`
`flaskapp | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)`
The environment variable FLASK\_APP is being read correctly, flask detects its ("`* Serving Flask app "`[`app.py`](https://app.py/)`"`"), but the config file isnt loaded ("`Debug mode: off`"), also `HOST = '`[`0.0.0.0`](https://0.0.0.0/)`'` is not set because i cant reach the app with my browser.

I assume that i have some error in my [app.py](https://app.py), but iam not sure.

Can you guys see any errors in my set up here? Wanted to learn flask and now already stuck with the basics :(

Btw, i also tried it without docker (same python files), also no luck.

Thanks in advance, have a nice day everyone!

/r/flask
https://redd.it/8p2ygx
Advice on IDE or editor for Django

I'm new at Django and currently working on a large personal project (iOS chat app with django backend). I'm using Webfaction hosting, and right now I'm just logging in through ssh, using vim to edit files, then running and opening up the error log file to debug one error at a time. Is there better way of doing this? It'd be nice to be able to use a Python "compiling" editor and run the code immediately without having to ssh files back and forth.

/r/django
https://redd.it/8p6zoa
Easiest way to query a model's property?

In my user model, I have a paid member ("premium") property that checks Stripe and returns True or False.

This is fine for 90% of my site and locking away content, but there are also times when I need to sort users by premium status. Any suggestions for how to do this?

I saw something along the lines of this:

class UserProfile(models.model):
premium_field = models.BooleanField(db_column="premium")

@property
def premium(self):
if stripe.paid():
return True
else:
return False


But it looks like I might need a getter function as well? Hopefully I'm on the right track -- I just don't want to migrate or push changes unless I know I have it right. Thanks!



/r/djangolearning
https://redd.it/8p57w5
Is it a good idea to use a mixin for a multi-value field?

Lets say that I have a model called 'Movie' and I want 'Movies' to have a spot to remember all the genres it belongs to (Horror, Suspense, Funny, Romance, etc... ). It seems like there are a few options. I could make a single field that stores a JSON string that holds genres as keys and booleans as values (Bad Idea). I could create a Boolean field in 'Movie' for each of the genres. But that makes for a lot of fields in a single model. Or I could make a mixin called 'GenreMixin' with a boolean field for each genre and have 'Movie' inherit from it.


What is the best practice, what will lead to the easiest to maintain down the road?

/r/djangolearning
https://redd.it/8p538x
[AF] How to run a function after returning response (AWS Lambda)

Hey everyone! I'm using zappa to run a simple ffmpeg transcode function on Lambda with Flask. While Lambda functions can run for up to 5 minutes, the problem I'm running into is that Amazon's API Gateway times out after 30 seconds so I'm unable to just wait for ffmpeg to finish before returning a response.

I'm looking for a way to return a response immediately from my Flask function and then continue running my ffmpeg function in the background. (The Flask response will contain a JSON payload with the S3 url that the finished ffmpeg job will upload to.)

I've tried using `threading.Thread` but it doesn't appear to stay alive after my Flask function returns. Thoughts/ideas? Thank you for your help!

/r/flask
https://redd.it/8p78ev