我试图使用BaseHttpServer创建一个http代理,它基于SocketServer,它有2个异步Mixins(ThreadingMixIn和ForkingMixIn)
他们在每个请求上工作的那两个问题(为每个请求分配一个新线程或fork一个新的子进程)
是否有一个Mixin利用一个让我们说4个子进程和每个40个线程的池,所以请求由那些已经创建的线程处理?
因为这会带来巨大的性能提升,我想这会节省一些资源.
python concurrency asyncsocket socketserver python-multithreading
我正在构建一个使用Flask和Scrapy的应用程序.访问我的应用程序的根URL时,它会处理一些数据并显示它.另外,如果它还没有运行,我也想(重新)启动我的蜘蛛.由于我的蜘蛛需要大约1.5小时才能完成运行,因此我使用线程将其作为后台进程运行.这是一个最小的例子(你还需要testspiders):
import os
from flask import Flask, render_template
import threading
from twisted.internet import reactor
from scrapy import log, signals
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from testspiders.spiders.followall import FollowAllSpider
def crawl():
spider = FollowAllSpider(domain='scrapinghub.com')
crawler = Crawler(Settings())
crawler.configure()
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run()
app = Flask(__name__)
@app.route('/')
def main():
run_in_bg = threading.Thread(target=crawl, name='crawler')
thread_names = [t.name for t in threading.enumerate() if isinstance(t, threading.Thread)]
if 'crawler' not in thread_names:
run_in_bg.start() …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序启动一系列线程.有时,其中一个线程死亡(通常是由于网络问题).如何正确检测线程崩溃并重新启动该线程?这是示例代码:
import random
import threading
import time
class MyThread(threading.Thread):
def __init__(self, pass_value):
super(MyThread, self).__init__()
self.running = False
self.value = pass_value
def run(self):
self.running = True
while self.running:
time.sleep(0.25)
rand = random.randint(0,10)
print threading.current_thread().name, rand, self.value
if rand == 4:
raise ValueError('Returned 4!')
if __name__ == '__main__':
group1 = []
group2 = []
for g in range(4):
group1.append(MyThread(g))
group2.append(MyThread(g+20))
for m in group1:
m.start()
print "Now start second wave..."
for p in group2:
p.start()
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我启动4个线程然后我开始4个线程.每个线程随机生成int介于0和10之间.如果int是4,则会引发异常.请注意,我没有 …
首先,我不确定是否应将此作为Ubuntu问题发布或在此处.但我猜它更像是一个Python问题,而不是一个OS问题.
我的Python应用程序在64核AMD服务器上运行在Ubuntu之上.它通过调用.so来从网络上的5 GigE摄像机中提取图像ctypes,然后处理它们.我看到我的应用程序经常暂停,导致相机的帧被外部相机库丢弃.
为了调试这个,我使用了流行的psutilPython包,我在一个单独的线程中每0.2秒注销一次CPU统计数据.我在该线程中睡眠0.2秒,当睡眠时间长得多时,我也看到相机帧被丢弃.我看到长达17秒的停顿!我的大多数处理是在OpenCV或Numpy(两者都发布GIL)或应用程序的一部分中multiprocessing.Pool
有59个进程(这是为了绕过Python GIL).
当暂停发生时,我的调试日志记录在我的许多进程'线程上显示非常高的'系统'(即内核)CPU时间.
例如.我看到CPU时间如下(通常每0.2秒),然后突然大跳('进程'数字在CPU利用率,即1个CPU完全使用将是1,Linux top显示123%将是1.2):
Process user | Process system | OS system % | OS idle %
19.9 | 10.5 | 6 | 74
5.6 | 2.3 | 4 | 87
6.8 | 1.7 | 11 | 75
4.6 | 5.5 | 43 | 52
0.5 | 26.4 | 4 | 90
Run Code Online (Sandbox Code Playgroud)
我不知道为什么在匹配高流程系统使用之前报告一行高OS系统使用情况.两者相比,64核中的26.4 = 41%.此时,我的应用程序经历了大约3.5秒的暂停(由我的CPU信息记录线程使用OpenCV确定,cv2.getTickCount()以及Python日志记录输出中的时间戳跳转)导致多个摄像机帧被丢弃.
发生这种情况时,我还记录了我的进程的每个线程的CPU信息.对于上面的示例,25个线程在"系统"CPU利用率为0.9时运行,并且在0.6处运行更多,这与上面26.4的进程的总数相匹配.那时大约有183个线程在运行.
在使用多处理池(它用于短脉冲串)之后,这种暂停通常似乎很接近,但每次使用池时都不会发生.此外,如果我将需要在池外进行的处理量减半,则不会发生相机跳过.
问题:如何确定操作系统'系统'/内核时间突然出现的原因?为什么会在Python应用程序中发生?
更重要的是:任何想法为什么会发生这种情况以及如何避免它?
笔记:
upstartrespawn在upstart中使用)并且这种情况每天发生多次,因此不是由于长时间运行,我也看到这在进程开始后很快发生python linux performance python-multithreading python-multiprocessing
我目前在python中使用线程函数并得到以下内容:
In [1]:
import threading
threading.activeCount()
Out[1]:
4
Run Code Online (Sandbox Code Playgroud)
现在在我的终端上,我使用lscpu并了解到每个核心有2个线程,我可以访问4个核心:
kitty@FelineFortress:~$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 60
Stepping: 3
CPU MHz: 800.000
BogoMIPS: 5786.45
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
NUMA node0 CPU(s): 0-7
Run Code Online (Sandbox Code Playgroud)
因此,我应该有超过4个线程来访问.是否有一个python函数,我可以用来增加我使用的核心数量(例如)以获得超过4个线程?或者甚至可以在启动ipython笔记本时在终端上键入内容,如下所示:
ipython notebook …Run Code Online (Sandbox Code Playgroud) 我有一个简单的生产者消费者应用程
生产者是一个将东西写入队列的线程,而消费者是一个线程,它从队列中读取消息并在某些点退出.
我的制片人看起来有点像这样
def producer(queue):
while not queue.full():
queue.put(randint(1, 199))
Run Code Online (Sandbox Code Playgroud)
和消费者
def consumer(queue):
for i in range(100):
print(queue.get())
queue.task_done()
Run Code Online (Sandbox Code Playgroud)
在我的主要部分,我调用那样的线程
p = Thread(target=producer)
c = Thread(target=consumer)
p.daemon = True
p.start()
c.start()
c.join()
Run Code Online (Sandbox Code Playgroud)
当c完成唯一剩下的非deamon线程是main时,结束这些线程的正确方法是什么?
更新
这是我的生产者正在使用的确切代码,因为消费者正在退出,问题在于生产者
def generate_random_alphanumerics(msg_queue):
while True:
if not msg_queue.full():
msg_queue.put(hashlib.sha1(bytes(randint(1, 10000))).hexdigest() * 10)
else:
sleep(0.01)
Run Code Online (Sandbox Code Playgroud)
是线程正在睡觉的问题?
在编写多线程代码时,我已经阅读了很多关于这个python GIL业务的"糟糕"的内容,但我从未见过一个例子.有人可以给我一个基本的例子,说明GIL在使用线程时何时会出现问题.
谢谢!
asyncio多年编写多线程代码后,我正在练习。
注意到我觉得很奇怪的东西。无论是在asyncio和concurrent有一个Future对象。
from asyncio import Future
from concurrent.futures import Future
Run Code Online (Sandbox Code Playgroud)
猜猜每个人都有自己的角色。
我的问题是我是否可以转移concurrent.future.Future到asyncio.Future(或相反)?
python-multithreading python-3.x python-asyncio concurrent.futures
我有以下代码使用threading并打印当前计数。
import threading
count = 0
def worker():
"""thread worker function"""
global count
count += 1
print(count)
threads = []
for i in range(5):
t = threading.Thread(target=worker)
threads.append(t)
t.start()
Run Code Online (Sandbox Code Playgroud)
当前设置为 5 个线程。我怎样才能让它继续运行线程直到达到某个#。即以 5 个线程运行,直到worker()运行 100 次。
我对开发网络应用程序还比较陌生。
我希望收到您对以下架构考虑因素的意见和改进建议。
我使用 CLIPS 开发了一个专家系统 ES。现在我计划将其作为 Web 应用程序提供给我们公司的各种用户。在开始讨论更多细节之前,我目前正在考虑应该涉及哪些技术。
目标是网络应用程序的用户面对一个类似聊天的动画,引导他或她向 ES 提供越来越多的输入,从而获得最终结果。
经过我自己的一些研究后,我提出了以下想法
在后端我使用 PyCLIPS 作为 Python 和 CLIPS 之间的接口
然后我使用 DJANGO 将我的 python 代码集成到网页中,动态改变用户和 ES 之间的聊天。
有一件事情至今仍然困扰着我:如何管理众多并发用户?我应该使用一个 ES,每个用户都有一组单独的事实,还是每个用户都应该有他或她自己的 ES 实例?
对于这个问题,您还有其他比这个更好的高级方法吗?
我期待您就此事提供经验和意见。
最好的
python ×9
python-3.x ×2
asyncsocket ×1
clips ×1
concurrency ×1
django ×1
flask ×1
gil ×1
linux ×1
performance ×1
restart ×1
scrapy ×1
socketserver ×1
unix ×1