for excel_file in excel_files[:10]: # save_stock_excel_to_mongodb(db, excel_file) with ThreadPoolExecutor( max_workers=10 ) as t: # 创建一个最大容纳数量为5的线程池 all_task = [t.submit( save_stock_excel_to_mongodb, db, excel_file ) ] # 函数循环 wait( all_task, return_when=FIRST_COMPLETED ) for future in as_completed( all_task ): data = future.result() 上述代码没有多线程运行
时间: 2024-02-04 19:04:03 浏览: 33
您的代码似乎存在问题,其中的线程池并没有起到多线程处理的作用。原因在于您的线程池的创建和提交都在 for 循环中进行,这会导致每次循环都会创建一个新的线程池。因此,建议将线程池的创建移动到循环外面,只需创建一次即可。同时,需要使用 t.map 方法将 save_stock_excel_to_mongodb 函数和 excel_files 列表传递给线程池进行处理。下面是修改后的代码:
```
with ThreadPoolExecutor(max_workers=10) as t: # 创建一个最大容纳数量为10的线程池
all_task = t.map(save_stock_excel_to_mongodb, [db]*10, excel_files[:10]) # 使用 t.map 方法进行处理
for data in all_task:
# 处理返回结果
pass
```
这样修改后的代码应该可以实现多线程处理了。
相关问题
mongodb_exporter: error: unknown flag --config.file
This error occurs when you are trying to use the flag "--config.file" with the "mongodb_exporter" command, but the flag is not recognized by the command.
Here are a few possible solutions:
1. Check the version of the "mongodb_exporter" command you are using. The "--config.file" flag may not be available in older versions. You can check the documentation for the version you are using to see if the flag is supported.
2. Make sure you are using the correct syntax for the flag. The correct syntax is "--config.file=path/to/config/file". If you are missing the equal sign or the path to the file, the flag may not be recognized.
3. Try using a different flag to specify the configuration file. The "--web.config" flag is a common alternative that is supported by many exporters, including the mongodb_exporter.
4. If none of the above solutions work, you can try updating or reinstalling the mongodb_exporter command to ensure that you have the latest version and all necessary dependencies.
java mongodb 查询 游标_MongoDB find()方法:查询数据
MongoDB的`find()`方法是用来查询数据的,它可以返回查询结果的游标。游标是一个指向查询结果集的指针,通过它可以逐个访问查询结果集中的文档。
在Java中,使用MongoDB驱动程序可以调用`find()`方法来查询数据,示例代码如下:
```java
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testDB");
MongoCollection<Document> collection = database.getCollection("testCollection");
// 创建查询条件
Document query = new Document("name", "John");
// 查询数据
FindIterable<Document> result = collection.find(query);
// 遍历查询结果的游标,输出文档内容
for (Document doc : result) {
System.out.println(doc.toJson());
}
```
在该示例中,首先创建了MongoDB客户端,然后获取了一个数据库和一个集合对象。接着创建了一个查询条件,查询条件是`name`字段等于`John`。最后调用`find()`方法查询数据,并遍历游标输出查询结果集中的每个文档。
需要注意的是,`find()`方法只是查询数据,还需要通过游标来获取查询结果集中的文档。游标是一个迭代器对象,可以通过`iterator()`方法来获取。此外,还可以使用`limit()`方法限制查询结果集的大小,使用`skip()`方法跳过查询结果集中的前几个文档,以及使用`sort()`方法对查询结果集进行排序等操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)