我有一个python脚本,所以我在python中使用线程模块并发执行。
class check_bl(threading.Thread):
def __init__(self, ip_client, reverse_ip, key, value):
threading.Thread.__init__(self)
self.ip_client = ip_client
self.reverse_ip = reverse_ip
self.key = key
self.value = value
def run(self):db=MySQLdb。connect('localhost','mytable','user','mytable')游标=db。cursor()query=“dig short”str(反向ip)”按键try:output=子进程。检查\u输出(查询,shell=True)输出\u编辑=输出。条带()除外:输出\u edited=“发生超时”
if output_edited in value:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
else:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
对于IPNetwork(range_ip_client)中的ip_client:ip_client=......reverse_ip=...
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
threadList.append(myThread)
for t in threadList:
t.join()
问题是my_dictionary中的每个键、值都没有执行(没有插入到数据库,尽管某些键、值执行了多次)。我的字典有30个条目,但只有10-20个条目可以执行。如果我像下面的2个例子那样插入join(),我的所有字典项()都会执行,但同时只执行一个线程。如果创建的每个线程都有问题,那么它将创建一个数据库连接。所以数据库无法处理它。
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
threadList.append(myThread)
for t in threadList:
t.join()
或者
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
t.join()
谢谢Dksj,我的问题解决了。也许类方法有问题,只需要定义一个函数。
def check_bl(ip_client, reverse_ip, key, value):
db = MySQLdb.connect('localhost', 'mytable', 'user', 'mytable')
cursor = db.cursor()
query = "dig +short " + str(reverse_ip) + "." + key
try:
output = subprocess.check_output(query, shell=True)
output_edited = output.strip()
except:
output_edited = "Timeout occured"
if output_edited in value:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
else:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
for ip_client in IPNetwork(range_ip_client):
ip_client = ....
reverse_ip = ...
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
threadList.append(myThread)
[t.start() for t in threadList]
[t.join() for t in threadList]
我的问题与你的略有不同,我没有使用类方法,我只是根据我的代码修改了你的代码,你可以试试。这个链接帮助我解决了我的问题:如何在Python中使用线程?
def check_bl(ip_客户端、反向ip、密钥、值):。。。。
threads []
for key, value in my_dictionary.items():
# Instantiates the thread
myThread = Thread( target=check_bl, args=(ip_client, reverse_ip, key, value) )
#add thread to list
threads.append(myThread)
[th.start() for th in threads]
#Join all the threads to make sure the parent waits for all of them to finish
[th.join() for th in threads]
如果每个任务(线程)有多个阶段。那么如何确保所有任务执行第1阶段,然后执行第2阶段,依此类推。例如如何修改下面的代码,使我的输出为:Task1的第1阶段、Task2的第1阶段、Task3的第1阶段、Task1的第2阶段、Task2的第2阶段、Task3的第2阶段,依此类推...
更新了问题和图像。 是否有任何方法可以暂停所有线程,直到任何线程执行samplerA为止(不管线程数是多少,这只需要执行一次),在执行这个sampler之后,所有线程都可以继续执行。单击以获取图像
创建并执行内核线程 建立进程控制块(proc.c中的alloc_proc函数)后,现在就可以通过进程控制块来创建具体的进程/线程了。首先,考虑最简单的内核线程,它通常只是内核中的一小段代码或者函数,没有自己的“专属”空间。这是由于在uCore OS启动后,已经对整个内核内存空间进行了管理,通过设置页表建立了内核虚拟空间(即boot_cr3指向的二级页表描述的空间)。所以uCore OS内核中的所有
这可能是在类似的背景下问的,但我在搜索了大约20分钟后找不到答案,所以我会问。 我已经编写了一个Python脚本(比如说:scriptA.py)和一个脚本(比如说scriptB.py) 在scriptB中,我想用不同的参数多次调用scriptA,每次运行大约需要一个小时,(这是一个巨大的脚本,做了很多事情……不用担心),我希望能够同时使用所有不同的参数运行scriptA,但我需要等到所有参数都完成
问题内容: 我下面有一段代码,该代码创建了几个线程来执行任务,而单独运行效果很好。但是,我很难理解为什么在所有线程完成并调用该语句之前,我在函数中调用的打印语句不会执行。我希望它们在线程执行时被调用。有没有简单的方法可以做到这一点,为什么首先要这样做呢? 问题答案: 这是由于stdout缓冲引起的。您仍然可以刷新缓冲区: 您可以在此处和此处找到有关此问题的更多信息。
有没有可能使用一组线程池来共享大型线程池中的线程,而不是创建新的线程? 在我们的RESTful API应用程序中,一个请求可能涉及多个并行任务。为了提高性能,我们希望在具有固定数量(比如200个)线程的线程池中执行并行任务。但是我们也希望限制每个请求可以使用的最大线程数。所以我在想,如果有可能为每个请求创建一个具有最大池大小的子线程池,它不会自己创建线程,而是尝试从全局线程池中获取一个新线程,并在