V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zy820
V2EX  ›  Python

socketserver.server_forever()+多线程处理数据并存入数据库

  •  
  •   zy820 · Aug 3, 2018 · 3442 views
    This topic created in 2835 days ago, the information mentioned may be changed or developed.

    我现在在用 socketserver.server_forever()处理请求,在 handle ()里面接受数据存入 queue,现在想用另外个线程取出 queue 数据并存入数据库,想问这个新线程在哪里开启呢?发现 server_forever 一直运行,新线程启动不了 if name == "main": HOST, PORT = "0.0.0.0", 9999 # Linux server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) # 线程 server.serve_forever() connect_sql() init_db() for i in range(multiprocessing.cpu_count()): t = threading.Thread(target=getsensor_que) t.start()

    1 replies    2018-08-03 10:57:35 +08:00
    zy820
        1
    zy820  
    OP
       Aug 3, 2018
    if __name__ == "__main__":
    sensor_que = queue.Queue()
    sensor_data = {'DeviceId': '', 'AirPressure': 0, 'Humidity': 0, 'Noise': 0, 'Pm25': 0, 'Temperature': 0,
    'WindDirection': 0, 'WindSpeed': 0}
    # HOST, PORT = "localhost", 9999 #windows
    HOST, PORT = "0.0.0.0", 9999 # Linux
    server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) # 线程
    server.serve_forever()
    print('before init_db!')
    connect_sql()
    init_db()
    print('after init_db!')
    for i in range(multiprocessing.cpu_count()):
    t = threading.Thread(target=getsensor_que)
    t.start()
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1004 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:17 · PVG 03:17 · LAX 12:17 · JFK 15:17
    ♥ Do have faith in what you're doing.