# 多线程、多进程相关 thread_event = threading.Event() pools = multiprocessing.Pool(10) # 进程,最多同时不会超过10个 meic 7 + ncep 2 # inputday_str = '2023-04-13' inputday_str = args.day inputday_dt = datetime.strptime(inputday_str,'%Y-%m-%d') # 前一天才是目标日 desday_dt = inputday_dt + timedelta(days=-1) desday_str = desday_dt.strftime('%Y-%m-%d') # NOTE 1: 依据当前时间创建文件对应日期csv,处理qgzd的数据,也就是target数据 create_time_csv(desday_dt) read_qgzd_raw(desday_str) # NOTE 2: 生成两批日期 full_time_list = pd.date_range(desday_str,periods=3).strftime('%Y-%m-%d').tolist() temptime = (desday_dt + timedelta(days=3)).strftime('%Y-%m-%d') one_time_list = pd.date_range(temptime,periods=4).strftime('%Y-%m-%d').tolist()
时间: 2024-04-18 20:24:05 浏览: 31
这段代码涉及多线程和多进程相关的操作。首先,创建了一个线程事件对象 `thread_event`,然后使用 `multiprocessing.Pool` 创建了一个进程池 `pools`,最多同时运行10个进程。
接下来,根据输入的日期字符串 `inputday_str`,将其转换为 `datetime` 对象 `inputday_dt`。然后,通过将目标日期减去一天,得到前一天的日期 `desday_dt`,并将其转换为字符串 `desday_str`。
接下来的代码主要包括两个注意事项:
1. 根据当前时间创建一个对应日期的 CSV 文件,并处理 qgzd 数据(目标数据)。具体的函数调用是 `create_time_csv(desday_dt)` 和 `read_qgzd_raw(desday_str)`。
2. 生成两批日期列表。首先,使用 `pd.date_range` 生成一个包含 3 个日期的完整时间列表 `full_time_list`,这些日期从目标日期开始。然后,通过将目标日期加上 3 天,生成另外一个日期列表 `one_time_list`,其中包含 4 个日期。
以上是这段代码的大致解释。如果你有关于具体部分的问题,请继续提问。
相关问题
python中,wait_thread = threading.Thread(target=wait)换成multiprocessing使用
将 `threading.Thread` 替换为 `multiprocessing.Process` 即可使用 `multiprocessing` 模块。
示例代码如下:
```python
import multiprocessing
def wait():
"""等待用户输入"""
print("Start waiting...")
input()
print("End waiting!")
if __name__ == "__main__":
# 创建进程对象
wait_process = multiprocessing.Process(target=wait)
# 启动进程
wait_process.start()
# 等待进程结束
wait_process.join()
```
这个程序会创建一个进程,调用 `wait()` 函数进行等待用户输入。和 `threading` 模块不同的是,`multiprocessing` 模块需要在 `if __name__ == "__main__":` 中创建进程对象并启动进程,以避免进程被重复创建。
`multiprocessing.Process` 类也有一个 `join` 方法,可以等待进程结束。
下面这段代码报错(import threading import pandas as pd import numpy as np def thread1_func():#####接受挠度数据 global a a=5 event.wait() # 等待线程1完成 def thread2_func():#####接受应变数据 global c c=5 event.wait() # 等待线程1完成 thread1 = threading.Thread(target=thread1_func) thread2 = threading.Thread(target=thread2_func) thread1.start() thread2.start() thread1.join() thread2.join() b=a+c)
这段代码报错的原因是变量 `event` 未定义。在代码中使用 `event.wait()` 时,需要先定义并初始化 `event` 变量。可以使用 `threading.Event()` 来创建一个新的 `event` 对象,然后在 `thread1_func()` 和 `thread2_func()` 中使用该对象。以下是修改后的代码:
```python
import threading
import pandas as pd
import numpy as np
event = threading.Event()
def thread1_func():
global a
a = 5
event.wait() # 等待线程1完成
def thread2_func():
global c
c = 5
event.wait() # 等待线程1完成
thread1 = threading.Thread(target=thread1_func)
thread2 = threading.Thread(target=thread2_func)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
b = a + c
```
这样修改后,代码应该能够正常运行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)