結論
pythonanywhereではAPSchedulerを使用するとエラーが発生するようです。
定期実行についてはpythonanywhereに備わっているタスクスケジューラを使用したほうが良さそうです。
実際に起きたエラー
Djangoで作成したwebアプリにAPSchedulerで定期実行をする処理を入れてました。そのアプリをpywhonanywhereにデプロイしてwebアプリを確認したところ、以下のようなエラー画面が表示されました。
エラーログ(折りたたんでます)
error.log
2023-05-19 22:05:42,355: OSError: write error
2023-06-03 19:49:58,066: Error running WSGI application
2023-06-03 19:49:58,082: RuntimeError: The scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the scheduler to work.
2023-06-03 19:49:58,082: File "/var/www/kabuto64425_pythonanywhere_com_wsgi.py", line 15, in <module>
2023-06-03 19:49:58,082: application = get_wsgi_application()
2023-06-03 19:49:58,082:
2023-06-03 19:49:58,083: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-06-03 19:49:58,083: django.setup(set_prefix=False)
2023-06-03 19:49:58,083:
2023-06-03 19:49:58,083: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2023-06-03 19:49:58,083: apps.populate(settings.INSTALLED_APPS)
2023-06-03 19:49:58,083:
2023-06-03 19:49:58,084: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/apps/registry.py", line 120, in populate
2023-06-03 19:49:58,084: app_config.ready()
2023-06-03 19:49:58,084:
2023-06-03 19:49:58,084: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/apps.py", line 14, in ready
2023-06-03 19:49:58,084: start()
2023-06-03 19:49:58,084:
2023-06-03 19:49:58,084: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/ap_scheduler.py", line 62, in start
2023-06-03 19:49:58,084: scheduler.start()
2023-06-03 19:49:58,085:
2023-06-03 19:49:58,085: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/background.py", line 35, in start
2023-06-03 19:49:58,085: BaseScheduler.start(self, *args, **kwargs)
2023-06-03 19:49:58,085:
2023-06-03 19:49:58,085: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 145, in start
2023-06-03 19:49:58,085: self._check_uwsgi()
2023-06-03 19:49:58,085:
2023-06-03 19:49:58,086: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 843, in _check_uwsgi
2023-06-03 19:49:58,086: raise RuntimeError('The scheduler seems to be running under uWSGI, but threads have '
2023-06-03 19:49:58,086: ***************************************************
2023-06-03 19:49:58,086: If you're seeing an import error and don't know why,
2023-06-03 19:49:58,086: we have a dedicated help page to help you debug:
2023-06-03 19:49:58,086: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-06-03 19:49:58,086: ***************************************************
2023-06-03 19:54:04,626: Error running WSGI application
2023-06-03 19:54:04,642: RuntimeError: The scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the scheduler to work.
2023-06-03 19:54:04,642: File "/var/www/kabuto64425_pythonanywhere_com_wsgi.py", line 15, in <module>
2023-06-03 19:54:04,642: application = get_wsgi_application()
2023-06-03 19:54:04,643:
2023-06-03 19:54:04,643: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-06-03 19:54:04,643: django.setup(set_prefix=False)
2023-06-03 19:54:04,643:
2023-06-03 19:54:04,643: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2023-06-03 19:54:04,643: apps.populate(settings.INSTALLED_APPS)
2023-06-03 19:54:04,643:
2023-06-03 19:54:04,644: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/apps/registry.py", line 120, in populate
2023-06-03 19:54:04,644: app_config.ready()
2023-06-03 19:54:04,644:
2023-06-03 19:54:04,644: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/apps.py", line 14, in ready
2023-06-03 19:54:04,644: start()
2023-06-03 19:54:04,644:
2023-06-03 19:54:04,645: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/ap_scheduler.py", line 62, in start
2023-06-03 19:54:04,645: scheduler.start()
2023-06-03 19:54:04,645:
2023-06-03 19:54:04,645: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/background.py", line 35, in start
2023-06-03 19:54:04,645: BaseScheduler.start(self, *args, **kwargs)
2023-06-03 19:54:04,645:
2023-06-03 19:54:04,646: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 145, in start
2023-06-03 19:54:04,646: self._check_uwsgi()
2023-06-03 19:54:04,646:
2023-06-03 19:54:04,646: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 843, in _check_uwsgi
2023-06-03 19:54:04,646: raise RuntimeError('The scheduler seems to be running under uWSGI, but threads have '
2023-06-03 19:54:04,646: ***************************************************
2023-06-03 19:54:04,647: If you're seeing an import error and don't know why,
2023-06-03 19:54:04,647: we have a dedicated help page to help you debug:
2023-06-03 19:54:04,647: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-06-03 19:54:04,647: ***************************************************
2023-06-04 11:49:34,662: Error running WSGI application
2023-06-04 11:49:34,672: RuntimeError: The scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the scheduler to work.
2023-06-04 11:49:34,672: File "/var/www/kabuto64425_pythonanywhere_com_wsgi.py", line 15, in <module>
2023-06-04 11:49:34,673: application = get_wsgi_application()
2023-06-04 11:49:34,673:
2023-06-04 11:49:34,673: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-06-04 11:49:34,673: django.setup(set_prefix=False)
2023-06-04 11:49:34,673:
2023-06-04 11:49:34,673: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2023-06-04 11:49:34,674: apps.populate(settings.INSTALLED_APPS)
2023-06-04 11:49:34,674:
2023-06-04 11:49:34,674: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/apps/registry.py", line 120, in populate
2023-06-04 11:49:34,674: app_config.ready()
2023-06-04 11:49:34,675:
2023-06-04 11:49:34,675: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/apps.py", line 14, in ready
2023-06-04 11:49:34,675: start()
2023-06-04 11:49:34,675:
2023-06-04 11:49:34,675: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/ap_scheduler.py", line 62, in start
2023-06-04 11:49:34,676: scheduler.start()
2023-06-04 11:49:34,676:
2023-06-04 11:49:34,676: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/background.py", line 35, in start
2023-06-04 11:49:34,676: BaseScheduler.start(self, *args, **kwargs)
2023-06-04 11:49:34,676:
2023-06-04 11:49:34,676: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 145, in start
2023-06-04 11:49:34,676: self._check_uwsgi()
2023-06-04 11:49:34,677:
2023-06-04 11:49:34,677: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 843, in _check_uwsgi
2023-06-04 11:49:34,677: raise RuntimeError('The scheduler seems to be running under uWSGI, but threads have '
2023-06-04 11:49:34,677: ***************************************************
2023-06-04 11:49:34,677: If you're seeing an import error and don't know why,
2023-06-04 11:49:34,677: we have a dedicated help page to help you debug:
2023-06-04 11:49:34,678: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-06-04 11:49:34,678: ***************************************************
2023-06-04 12:20:36,255: Error running WSGI application
2023-06-04 12:20:36,263: RuntimeError: The scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the scheduler to work.
2023-06-04 12:20:36,263: File "/var/www/kabuto64425_pythonanywhere_com_wsgi.py", line 15, in <module>
2023-06-04 12:20:36,263: application = get_wsgi_application()
2023-06-04 12:20:36,264:
2023-06-04 12:20:36,264: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-06-04 12:20:36,264: django.setup(set_prefix=False)
2023-06-04 12:20:36,264:
2023-06-04 12:20:36,264: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2023-06-04 12:20:36,264: apps.populate(settings.INSTALLED_APPS)
2023-06-04 12:20:36,265:
2023-06-04 12:20:36,265: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/apps/registry.py", line 120, in populate
2023-06-04 12:20:36,265: app_config.ready()
2023-06-04 12:20:36,265:
2023-06-04 12:20:36,265: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/apps.py", line 14, in ready
2023-06-04 12:20:36,265: start()
2023-06-04 12:20:36,265:
2023-06-04 12:20:36,265: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/ap_scheduler.py", line 62, in start
2023-06-04 12:20:36,266: scheduler.start()
2023-06-04 12:20:36,266:
2023-06-04 12:20:36,266: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/background.py", line 35, in start
2023-06-04 12:20:36,266: BaseScheduler.start(self, *args, **kwargs)
2023-06-04 12:20:36,266:
2023-06-04 12:20:36,267: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 145, in start
2023-06-04 12:20:36,267: self._check_uwsgi()
2023-06-04 12:20:36,267:
2023-06-04 12:20:36,267: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 843, in _check_uwsgi
2023-06-04 12:20:36,267: raise RuntimeError('The scheduler seems to be running under uWSGI, but threads have '
2023-06-04 12:20:36,267: ***************************************************
2023-06-04 12:20:36,268: If you're seeing an import error and don't know why,
2023-06-04 12:20:36,268: we have a dedicated help page to help you debug:
2023-06-04 12:20:36,268: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-06-04 12:20:36,268: ***************************************************
2023-06-04 12:20:37,623: Error running WSGI application
2023-06-04 12:20:37,624: RuntimeError: The scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the scheduler to work.
2023-06-04 12:20:37,624: File "/var/www/kabuto64425_pythonanywhere_com_wsgi.py", line 15, in <module>
2023-06-04 12:20:37,625: application = get_wsgi_application()
2023-06-04 12:20:37,625:
2023-06-04 12:20:37,625: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-06-04 12:20:37,625: django.setup(set_prefix=False)
2023-06-04 12:20:37,626:
2023-06-04 12:20:37,626: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2023-06-04 12:20:37,626: apps.populate(settings.INSTALLED_APPS)
2023-06-04 12:20:37,626:
2023-06-04 12:20:37,627: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/django/apps/registry.py", line 120, in populate
2023-06-04 12:20:37,627: app_config.ready()
2023-06-04 12:20:37,627:
2023-06-04 12:20:37,628: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/apps.py", line 14, in ready
2023-06-04 12:20:37,628: start()
2023-06-04 12:20:37,628:
2023-06-04 12:20:37,628: File "/home/kabuto64425/kabuto64425.pythonanywhere.com/app/ap_scheduler.py", line 62, in start
2023-06-04 12:20:37,629: scheduler.start()
2023-06-04 12:20:37,629:
2023-06-04 12:20:37,629: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/background.py", line 35, in start
2023-06-04 12:20:37,629: BaseScheduler.start(self, *args, **kwargs)
2023-06-04 12:20:37,629:
2023-06-04 12:20:37,630: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 145, in start
2023-06-04 12:20:37,630: self._check_uwsgi()
2023-06-04 12:20:37,630:
2023-06-04 12:20:37,630: File "/home/kabuto64425/.virtualenvs/kabuto64425.pythonanywhere.com/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 843, in _check_uwsgi
2023-06-04 12:20:37,631: raise RuntimeError('The scheduler seems to be running under uWSGI, but threads have '
2023-06-04 12:20:37,631: ***************************************************
2023-06-04 12:20:37,631: If you're seeing an import error and don't know why,
2023-06-04 12:20:37,631: we have a dedicated help page to help you debug:
2023-06-04 12:20:37,632: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-06-04 12:20:37,632: ***************************************************
調査
以下の記事を参考にしました。
この記事の内容としては、定期的なタスクを実行するためにプロセス内スケジューラを使用するのではなく、プラットフォームに組み込まれているスケジュールタスク機能を使用してくださいとのこと。
公式ドキュメントではないですが、実際にエラーも出ていることから、定期的なタスクを実行しようとするとエラーが出ると思っておいて良さそうです。
おわりに
pythonanywhereタスクスケジューラの使用方法については調査中です。
また分かり次第、別の記事にまとめていきたいです。
以上です。