Is there a way to connect django and a tcp server built with asyncio?
I have a TCP server that should maintain long-lasting connections with clients, but I want to integrate it with Django so that a user can send data over the TCP server based on forms from Django
I've heard of celery, but I do not know if it would be suitable for this application
My current idea is to put a temporary tcp client in the django code that receives posts, and have it send data to the tcp server. I would prefer not to do this because I would have to add more special cases to the TCP server in order to recognize that data is being sent from Django and not one of its other clients
Try aiohttp-wsgi. It offers a WSGI bridge for asyncio, on top of aiohttp, so you can process Django requests within your asyncio process.
Note that, when using Django as a web framework, the model instances lifecycle is usually not a problem because objects live only as long as the view that creates them. When using Django as a service (outside Django HTTP Requests) this doesn't happen, and you need to synchronize access to model instances carefully. This involves avoiding usage of cached/older instances to do modifications, and refreshing model objects anytime other process (ie Django views, or other service code) might have changed them. Django CRM does not guarantee uniqueness of model objects that represent the same database record.
Related
I am working and I need your help. I am working on a website with Django with DRF on the backend and react on the frontend. I need a way of sending api data to the client whenever any changes are made. The data is only coming from the database, the client is only viewing the progress. What options do I have to acheive. I need it to handle a large number on people. Say 20 000. Please your help will do a lot for me.
You have two options:
Websockets. See Django-channels for Django implementation, on React side you will have to setup Websocket connection to your Django and listen for messages.
Use long polling, or periodic polling of data. Basically this is a process when your React app sends requests to backend periodically or makes a request that lasts forever.
I want to build a Django app which listens to events from messaging apps and writes them in a database.
For eg, in Slack, on channel creation, I want to create a new entity in database and when a message is posted in that channel, I want to update that entity with the message.
I want the app to work similarly for other apps like Telegram, Discord etc.
How can I implement this using a common backend API for all the apps, i.e. a common API which listens to the events and updates the db ?
Or is there some third-party API available (for eg. Twilio maybe) which can fulfil my purpose ?
Iam working in django 2 with websockets inorder to pass the response to and fro a game server.Can anyone suggest the method or the correct flow for complete the communiction task between server and django through websockets?
If your server is already set up in django then django-channels would make sense. Websocket consumers in django channels have full access to your django ORM and your other views in django can issue messages (over channel groups) that can be used to inform the open websocket connections of changes they need to stream to the user.
How does django rest framework internally manage Database connection pool. Does it persist a DB connection or it is one DB connection for each DB call? Can we configure Database thread pool connection in Django ?
The Django REST framework simply uses Django's features regarding the db connectivity.
Django opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn’t usable any longer.
You can find much detail on the persistency of DB connections, which is a topic too broad to cover in here, on the relevant Django docs article.
I've built a JSON API in Django. I'd like to send real-time updates from an external service to Django to upsert a model.
I am really looking for insight on the best way to design the system with current/upcoming/active frameworks and tools. My thoughts are using node.js/Django/Foreman described below:
Existing Django JSON API
A node.js app, running via Foreman, that's subscribed to some external channel.
That channel sends node a JSON message
Node consumes message and makes an HTTP POST of JSON to a URL within my Django API.
Django API uses the JSON message to upsert a model within the Django application.
Now, it seems that I should be able to eliminate node.js from this equation, and have a service that lives "a little closer to home", home being the Django app, rather than having to cross HTTP.
Question being: Is the solution I have now an efficient approach, and is there a better way of doing things?
How do you need to subscribe to the other service? If the other service calls one of your Urls directly, just make Django listen there.
If the other service requires your side to act as server (non webserver, eg connects to you on some non web port) you will need to let a server run there, but again I wouldn't use Node but rather write a simple Python server (probably using the asynccore module), which you could start via foreman+manage.py and which would have access to models directly, eg wouldn't need to marshal the data into json just to send it to Django.
If you connect to the other service via a simple tcp connection I still would take the non node approach like described above.
P.S.: Don't bother to much about efficiency -- keep your system as simple as possible before developing over engineered solutions.