{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# **Scheduler**\n", "! pip install APScheduler\n", "1. Crontab 작업을 활용 (리눅스 기본)\n", "1. class sched.scheduler() 파이썬 기본모듈의 활용\n", "1. https://pypi.org/project/schedule/ 의 활용\n", "1. https://pypi.org/project/APScheduler/ 의 활용\n", "\n", "참고 블로그\n", "1. [파이썬 스케줄 수행 - schedule, apscheduler](https://blog.naver.com/PostView.nhn?blogId=varkiry05&logNo=221257249284&categoryNo=107&parentCategoryNo=0&viewDate=¤tPage=1&postListTopCurrentPage=1&from=postView)\n", "1. [파라미터 설명 블로그](https://www.programcreek.com/python/example/94838/apscheduler.schedulers.background.BackgroundScheduler)\n", "1. [Django 에 결합해서 활용](https://medium.com/@mrgrantanderson/replacing-cron-and-running-background-tasks-in-django-using-apscheduler-and-django-apscheduler-d562646c062e)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# %reset\n", "holiday_2020 = \"\"\"2020-01-01,2020-01.24,2020-01-27,2020-04-30,2020-05-01,\n", "2020-05-05,2020-09-30,2020-10-01,2020-10-02,2020-11-19,2020-12-25\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# **1 APScheduler**\n", "! pip install APScheduler\n", "\n", "## **01 실행 Interval 을 정의하기**\n", "실행 간격을 지정 (상대적 TimeSet)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def exec_interval(): \n", " print('인터벌 함수의 실행 : exec interval')\n", "\n", "# 예약방식 interval로 설정, 10초마다 한번 실행 \n", "from apscheduler.schedulers.blocking import BlockingScheduler\n", "sched = BlockingScheduler()\n", "sched.add_job(exec_interval, 'interval', seconds=1, id=\"test_2\")\n", "sched.add_job(exec_interval, 'interval', seconds=3, id=\"test_2\")\n", "# sched.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **02 실행 조건을 정의하기**\n", "절대적 Time Set" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def exec_cron(): \n", " print('Cron 함수의 실행 : exec cron')\n", "\n", "# cron 사용\n", "# id는 고유번호로 겹치면 => 'Job identifier (test_1) conflicts with an existing job'\n", "sched.add_job(exec_cron, 'cron', second='*/5', id=\"test_1\")\n", "sched.add_job(exec_cron, 'cron', minute='*/5', second='10,30')\n", "sched.add_job(exec_cron, 'cron', minute=\"59\", second='10', id=\"test_2\")\n", "sched.add_job(exec_cron, 'cron', day_of_week=\"0-5\", hour='23', minute='30')\n", "# ex) (5분)10,30초, (10분)10,30초, (15분)10,30초 : 스케줄링 시작\n", "sched.add_job(exec_cron, 'cron', day_of_week=\"0-6\", minute='*/5', second='10,30')\n", "# sched.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **03 실행 조건의 정의 2**\n", "파라미터로 실행조건 추가하기" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 파라미터를 받아서 실행하는 Job\n", "def test(test_str, test_str2):\n", " print(\"Hello: %s, %s \" % (test_str, test_str2))\n", "\n", "# args 에 값은 반드시 배열로 입력, 넣지 않으면 다음의 오류를 출력합니다\n", "# ValueError: The list of positional arguments is longer than the target callable can handle (allowed: 0, given in args: 2)\n", "sched.add_job(test, 'cron', minute=\"1\", second='*/5', id=\"test_11\", args=[\"테스트\", \"반가워요\"])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting requests-cache\n", " Downloading https://files.pythonhosted.org/packages/7f/55/9b1c40eb83c16d8fc79c5f6c2ffade04208b080670fbfc35e0a5effb5a92/requests_cache-0.5.2-py2.py3-none-any.whl\n", "Requirement already satisfied: requests>=1.1.0 in /home/momukji/Python/python/lib/python3.6/site-packages (from requests-cache)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/momukji/Python/python/lib/python3.6/site-packages (from requests>=1.1.0->requests-cache)\n", "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/momukji/Python/python/lib/python3.6/site-packages (from requests>=1.1.0->requests-cache)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/momukji/Python/python/lib/python3.6/site-packages (from requests>=1.1.0->requests-cache)\n", "Requirement already satisfied: idna<2.9,>=2.5 in /home/momukji/Python/python/lib/python3.6/site-packages (from requests>=1.1.0->requests-cache)\n", "Installing collected packages: requests-cache\n", "Successfully installed requests-cache-0.5.2\n" ] } ], "source": [ "! pip install requests-cache" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# **2 Running Python**\n", "! pip install APScheduler" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from muyong.stock import Krx, Think, Shinhan\n", "from muyong.util import Telegram\n", "t = Telegram()\n", "\n", "import requests_cache, os # requests Cache 로 속도 높이기\n", "from datetime import date\n", "\n", "def krx_crawler():\n", " requests_cache.install_cache('demo_cache')\n", " now = date.today()\n", " date_txt = now.strftime('%m%d')\n", " \n", " if now.weekday() not in [0,1,2,3,4]:\n", " print(\"today is not a Business day!\")\n", " return None\n", " \n", " else: # Business Day 인 경우\n", " codes = Krx().get_code()\n", " t.msg('-- 장 마감 후 거래원 수집시작 --')\n", " df = Think().get_trader_sql(codes) #, db_memory=True\n", " df.to_csv('csv/'+ date_txt + '_top_trader.csv', index=None)\n", " t.msg('상위 거래원 수집완료, 수집끝!')\n", " \n", " # 오늘이 금요일인 경우\n", " if now.weekday() == 4: \n", " df = Shinhan().get_trader_sql(codes) #, db_memory=True\n", " df.to_csv('csv/' + date_txt + '_all_trader.csv', index=None)\n", " t.msg('거래원별 거래량 수집완료')\n", " os.remove(\"demo_cache.sqlite\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from apscheduler.schedulers.blocking import BlockingScheduler\n", "sched = BlockingScheduler()\n", "# sched.add_job(krx_crawler, 'interval', seconds=1, id=\"test_2\")\n", "sched.add_job(krx_crawler, 'cron', day_of_week=\"0-5\", hour='15', minute='50', id=\"krx_1\")\n", "# sched.start()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'09-30-00-09-1569769200'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "date.today().strftime('%m-%d')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_name():\n", " return 'games'\n", "\n", "type(get_name())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'games:user-list'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "app_name = get_name()\n", "name = f'{app_name}:user-list'\n", "name" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.9" } }, "nbformat": 4, "nbformat_minor": 4 }