如何在烧瓶socketio应用程序中添加过滤器到tweepy流?

时间:2018-05-05 04:37:40

标签: stream tweepy flask-socketio

使用我的flask-socketio应用程序时,我正在尝试在收到新推文时更新所有连接的客户端。以下是我的代码。

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API

access_token = "tkndfkdj"
access_token_secret = "aldkjfd;"
consumer_key = "saldkjfa;"
consumer_secret = "dkaljfad"

app = Flask(__name__)
socketio = SocketIO(app,async=True)

tweet_count = 0

class MyListener(StreamListener):

    def on_data(self, data):
        print(data)
        return True

    def on_error(self, status):
        print(status)

@socketio.on('connect', namespace='/test')
def test_connect():
    print('client connected !!!')
    global myStream
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = API(auth)
    global myStream
    myStream = Stream(auth = api.auth, listener=MyListener())
    myStream.filter(track=['#python','#java'],async=True)

if __name__ == '__main__':
    socketio.run(app,host='0.0.0.0',port=9000, debug=True)

在上面的代码中,我在myStream.filter(track=['#python','#java'],async=True)函数中添加行test_connect后收到错误。

Exception in thread Thread-2:
Traceback (most recent call last):
  File "F:\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "F:\Anaconda3\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "F:\Anaconda3\lib\site-packages\tweepy\streaming.py", line 297, in _run
    six.reraise(*exc_info)
  File "F:\Anaconda3\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "F:\Anaconda3\lib\site-packages\tweepy\streaming.py", line 250, in _run
    verify=self.verify)
  File "F:\Anaconda3\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "F:\Anaconda3\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "F:\Anaconda3\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "F:\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "F:\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "F:\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "F:\Anaconda3\lib\site-packages\urllib3\connection.py", line 314, in connect
    cert_reqs=resolve_cert_reqs(self.cert_reqs),
  File "F:\Anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 269, in create_urllib3_context
    context.options |= options
  File "F:\Anaconda3\lib\ssl.py", line 465, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  File "F:\Anaconda3\lib\ssl.py", line 465, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  File "F:\Anaconda3\lib\ssl.py", line 465, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  [Previous line repeated 315 more times]
RecursionError: maximum recursion depth exceeded

我出错的任何想法?或者任何更好的架构来达到这个目的?

以下是pip freeze命令的输出

alabaster==0.7.10
anaconda-client==1.6.9
anaconda-navigator==1.7.0
anaconda-project==0.8.2
asn1crypto==0.24.0
asr-evaluation==2.0.2
astroid==1.6.1
astropy==2.0.3
attrs==17.4.0
audioread==2.1.5
Babel==2.5.3
backports.shutil-get-terminal-size==1.0.0
beautifulsoup4==4.6.0
bitarray==0.8.1
bkcharts==0.2
blaze==0.11.3
bleach==2.1.2
bokeh==0.12.13
boto==2.48.0
Bottleneck==1.2.1
certifi==2018.1.18
cffi==1.11.4
chardet==3.0.4
click==6.7
cloudpickle==0.5.2
clyent==1.2.2
colorama==0.3.9
comtypes==1.1.4
conda==4.4.10
conda-build==3.4.1
conda-verify==2.0.0
contextlib2==0.5.5
cryptography==2.1.4
cycler==0.10.0
Cython==0.27.3
cytoolz==0.9.0
dask==0.16.1
datashape==0.5.4
decorator==4.2.1
distributed==1.20.2
docutils==0.14
edit-distance==1.0.1
entrypoints==0.2.3
et-xmlfile==1.0.1
fastcache==1.0.2
filelock==2.0.13
Flask==1.0.2
Flask-Cors==3.0.3
Flask-Login==0.4.0
Flask-Session==0.3.1
Flask-SocketIO==3.0.0
gevent==1.2.2
glob2==0.6
greenlet==0.4.12
h5py==2.7.1
heapdict==1.0.0
html5lib==1.0.1
idna==2.6
imageio==2.2.0
imagesize==0.7.1
ipython==6.2.1
ipython-genutils==0.2.0
ipywidgets==7.1.1
isort==4.2.15
itsdangerous==0.24
jdcal==1.3
jedi==0.11.1
Jinja2==2.10
joblib==0.11
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.2.2
jupyter-console==5.2.0
jupyter-core==4.4.0
jupyterlab==0.31.4
jupyterlab-launcher==0.10.2
lazy-object-proxy==1.3.1
librosa==0.6.0
llvmlite==0.21.0
locket==0.2.0
lxml==4.1.1
MarkupSafe==0.23
matplotlib==2.1.2
mccabe==0.6.1
menuinst==1.4.11
mistune==0.8.3
mpmath==1.0.0
msgpack-python==0.5.1
multipledispatch==0.4.9
navigator-updater==0.1.0
nbconvert==5.3.1
nbformat==4.4.0
networkx==2.1
nltk==3.2.5
nose==1.3.7
notebook==5.4.0
numba==0.36.2
numexpr==2.6.4
numpy==1.14.0
numpydoc==0.7.0
oauthlib==2.0.7
odo==0.5.1
olefile==0.45.1
openpyxl==2.4.10
packaging==16.8
pandas==0.22.0
pandocfilters==1.4.2
parso==0.1.1
partd==0.3.8
path.py==10.5
pathlib2==2.3.0
patsy==0.5.0
pep8==1.7.1
pickleshare==0.7.4
Pillow==5.0.0
pkginfo==1.4.1
pluggy==0.6.0
ply==3.10
prompt-toolkit==1.0.15
psutil==5.4.3
py==1.5.2
PyAudio==0.2.11
pycodestyle==2.3.1
pycosat==0.6.3
pycparser==2.18
pycrypto==2.6.1
pycurl==7.43.0.1
pyflakes==1.6.0
Pygments==2.2.0
pylint==1.8.2
pyodbc==4.0.22
pyOpenSSL==17.5.0
pyparsing==2.2.0
PySocks==1.6.7
pytest==3.3.2
python-dateutil==2.6.1
python-engineio==2.1.0
python-socketio==1.9.0
pytz==2017.3
PyWavelets==0.5.2
pywin32==222
pywinpty==0.5
PyYAML==3.12
pyzmq==16.0.3
QtAwesome==0.4.4
qtconsole==4.3.1
QtPy==1.3.1
requests==2.18.4
requests-oauthlib==0.8.0
resampy==0.2.0
rope==0.10.7
ruamel-yaml==0.15.35
scikit-image==0.13.1
scikit-learn==0.19.1
scipy==1.0.0
seaborn==0.8.1
searchtweets==1.7.0
Send2Trash==1.4.2
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.11.0
snowballstemmer==1.2.1
sortedcollections==0.5.3
sortedcontainers==1.5.9
Sphinx==1.6.6
sphinxcontrib-websupport==1.0.1
spyder==3.2.6
SQLAlchemy==1.2.1
statsmodels==0.8.0
sympy==1.1.1
tables==3.4.2
tblib==1.3.2
termcolor==1.1.0
terminado==0.8.1
testpath==0.3.1
toolz==0.9.0
tornado==4.5.3
traitlets==4.3.2
tweepy==3.6.0
tweet-parser==1.13.0
TwitterSearch==1.0.2
typing==3.6.2
unicodecsv==0.14.1
urllib3==1.22
virtualenv==15.2.0
wcwidth==0.1.7
webencodings==0.5.1
Werkzeug==0.14.1
widgetsnbextension==3.1.0
win-inet-pton==1.0.1
win-unicode-console==0.5
wincertstore==0.2
wit==5.1.0
wrapt==1.10.11
xlrd==1.1.0
XlsxWriter==1.0.2
xlwings==0.11.5
xlwt==1.3.0
zict==0.1.3

1 个答案:

答案 0 :(得分:1)

由于您正在使用gevent并通过tweepy进行网络连接,因此您需要修补Python标准库,以使其与gevent的异步循环兼容。

这是您需要添加到猴子补丁的代码:

from gevent import monkey
monkey.patch_all()

这需要在您的主脚本中完成,并且高于所有其他导入。如果你已经进行了猴子修补,那么错误很可能是因为在其他导入之后进行猴子修补太晚了。