Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
to_datetime函数的Python多线程实现_Python_Multithreading_Pandas_String To Datetime - Fatal编程技术网

to_datetime函数的Python多线程实现

to_datetime函数的Python多线程实现,python,multithreading,pandas,string-to-datetime,Python,Multithreading,Pandas,String To Datetime,我需要对Python作业实现多线程 我有一个字典,字典中的每个键(大约40个)都是一个带时间戳的数据帧。大多数数据帧都有100000多行。它们的时间戳是%Y-%m-%d%H:%m:%S“格式的字符串 要转换时间戳字符串,我使用以下函数: def to_dt(df): df['timestamp'] = df['timestamp'].map(lambda n: pd.to_datetime(n, format='%Y-%m-%d %H:%M:%S')) return df 所以

我需要对Python作业实现多线程

我有一个字典,字典中的每个键(大约40个)都是一个带时间戳的数据帧。大多数数据帧都有100000多行。它们的时间戳是
%Y-%m-%d%H:%m:%S“
格式的字符串

要转换时间戳字符串,我使用以下函数:

def to_dt(df):
    df['timestamp'] = df['timestamp'].map(lambda n: pd.to_datetime(n, format='%Y-%m-%d %H:%M:%S'))
    return df
所以我想把每个进程
放到一个单独的线程中。我该怎么做

为了简化,让我们考虑下面的设置:

def to_dt(df):
    df['timestamp'] = df['timestamp'].map(lambda n: pd.to_datetime(n, format='%Y-%m-%d %H:%M:%S'))
    return df

# empty dictionary
d_test = {}

# dataframe with single string timestamp column
df = pd.DataFrame(columns=['st_dt'])

# populate dataframe with 1000 timestamp rows
for i in range(1000):
    df.loc[len(df)] = ['2018-10-02 10:00:00']

# add 20 instances of the dataframe to the dictionary with keys in format "a0" to 'a19'
for i in range(20):
    d_test['a'+str(i)] = df
现在,我们如何进行

范围(20)内的i的
:
to_dt(d_测试['a'+str(i)])


要在单独的线程中运行?

只需阅读Python中的线程只应在进程执行过程中出现某种等待时使用,例如连接到远程服务器或端口扫描等


在上述情况下,没有任何等待,因此不需要threadig。

由于GIL的存在,Python中任何时候都只有一个线程在运行,因此在这种情况下,多线程只会使性能更差

为了使用多核,您需要多处理而不是多线程,但是产生新进程的沉重开销肯定会超过其好处,因此在您的情况下,最好使用单个
pd.to_datetime


也很好地解释了GIL。

有了这样的多线程,您的进程会慢得多。基本上,您是说,在这种情况下,我最好不使用线程:我做对了吗?