# or, in absolute terms, instead of 275 ms it may take 220 ms. from gevent.monkey import patch_builtins, patch_contextvars, patch_thread, patch_time, patch_os, patch_queue, patch_select, \ patch_selectors, patch_signal, patch_socket, patch_ssl, patch_subprocess, patch_sys # Note that the order of patching matters, just like in patch_all patch_os() patch_time() patch_thread() patch_sys() patch_socket() patch_select() patch_selectors() patch_ssl() patch_subprocess() patch_builtins() patch_signal() patch_queue() patch_contextvars() # stdlib import locale import logging import os import ssl import sys from logging.config import dictConfig # ConcurrentLogHandler - updates stlidb's logging config on import so this needs to stay import cloghandler cloghandler = cloghandler # For pyflakes
from lib.util import human_format from exchange.bittrex import Bittrex from exchange.data_proxy import DataProxy import datetime import dateutil.parser import time import pandas as pd import gevent from gevent import monkey monkey.patch_time() monkey.patch_socket() monkey.patch_signal() monkey.patch_os() monkey.patch_dns() monkey.patch_sys() from exchange.data_proxy import DataProxy SPREAD_THRESHOLD = 0.1 def on_tick(pair, exchange): my_sell_order = 0 my_buy_order = 0 # run in loop outside 3s/tick data_api = DataProxy(exchange)
#!/usr/bin/env python # -*- coding: utf-8 -*- # brew install libevent # sudo easy_install greenlet # install gevent in pycharm # greenlet 基本思想: # 当一个greenlet遇到IO操作时,比如访问网络,就自动切换到其他的greenlet, # 等到IO操作完成,再在适当的时候切换回来继续执行 from gevent import monkey; monkey.patch_signal() import gevent, time def f(n): for i in range(n): print gevent.getcurrent(), i time.sleep(1) g1 = gevent.spawn(f,5) g2 = gevent.spawn(f,5) g3 = gevent.spawn(f,5) g1.join() g2.join() g3.join() # 运行以上代码, 可以看到,3个greenlet是依次运行而不是交替运行。 # 要让greenlet交替运行,可以通过gevent.sleep()交出控制权: print '\n使用gevent.sleep()让greenlet交替运行:'