Edit:
I am still unsure what you are trying to achieve and there are some important concerns commenters have highlighted. Firstly, the AppRegistryNotReady: Apps aren't loaded yet
error is really frustrating, I appreciate that. I am not sure how you have organised your project so can't provide an exact solution. It might be the way in which you have organised your files and from where you are importing your app's AppConfig class. Have you put it or any custom functions or classes in your app package level __init__.py
, so that you import from there? If so this seems to confuse Django, see https://stackoverflow.com/a/34136825/2275482
Secondly:
I'm trying to call function right after command python manage.py runserver
I think we are are struggling to understand what you are trying to achieve and whether or not you are conflating when your Django app starts with when the server starts. The web server is totally independent of Django as commenters have pointed out; it is a separate process.
Even the test server bundled with Django is independent of your Django app. It is just a simple development server put there for convenience but it isn't required and should not be used in production. Django is built in compliance with PEP 333. It will work, AFAIK, with any WSGI web server compliant with PEP 333, whether that is Apache and mod_wsgi, gunicorn or uWSGI etc etc. If you want code to be execute in relation to the server you are using you need to look into that server documentation.
manage.py
like django-admin
is just a utility to help with development see https://docs.djangoproject.com/en/2.1/ref/django-admin/. When you call manage.py runserver
you are invoking a utility which loads your application and setups a test server. You can see the flow of execution, what utilities it is importing and so on in the file itself. This execution flow is not the same as what would happen when using a production server.
A production server, as far as I know, loads your django app from the root wsgi.py
file. It won't even look at manage.py
So this goes back to are you trying to post a message after your server starts, In which case this is independent of Django or, when your Django app starts?
If you are trying to print things after django app starts, is initiated and loaded into the server, then you have a number of options. If you really wanted to, you could edit and add to manage.py
but I wouldn't tamper with Django's core beyond that. This will be totally ignored when using a different production server.
If you want to edit the entry point which will be loaded regardless of the server being used then you can edit the project's wsgi.py
module or the individual app AppConfig ready()
method.
If you want to build something independent of Django which continuously runs in then background then as @bruno-desthuilliers has pointed out, you need to build a separate process either as a cron job or using celery or even django-carrot if you wanted to, although the latter is still linked to the app starting but can work as a separate worker (I think). Django-carrot only has very simple features for small jobs..
I am sorry if this isn't of any help, best I can do with info provided.
I faced a similar issue when I wanted something to run inside the ready()
method of my apps AppConfig. For me it was the way in which my app was installed in my INSTALLED_APPS
setting variable.
Normally you would register your apps like so:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
]
However this will load the base AppConfig class for registering your apps. If you customise and override the app's AppConfig, so that you can declare your own ready()
method, which will be executed once the app has been instantiated, you need to refer to the overridden AppConfig directly in the INSTALLED_APPS
So
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app.apps.MyAPPConfig', # MyAppConfig is the config class that inherits from AppConfig
]
Alternatively, if you do not want this do this when a specific app is loaded via it's AppConfig, so not through the ready()
method, but rather literally when the project loads into the server you can place a in your project wsgi.py
module after the app loads
i.e
application = get_wsgi_application()
print('Hello World')
Hope that helps