Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day13最后一个例子感觉计时部分不太对啊。。计时前是不是线程就已经开始启动了? #1008

Open
Talonl opened this issue Jan 24, 2024 · 14 comments

Comments

@Talonl
Copy link

Talonl commented Jan 24, 2024

如下,如果我简单改下流程,让start后sleep5秒,实际计算时间就成0.08秒了,这显然不对吧。。

from multiprocessing import Process
from time import time, sleep


def task_handler():
    sleep(5)


def main():
    processes = []
    # 启动8个进程将数据切片后进行运算
    for _ in range(8):
        p = Process(target=task_handler)
        processes.append(p)
        p.start()
    # 开始记录所有进程执行完成花费的时间
    sleep(5)
    start = time()
    for p in processes:
        p.join()
    end = time()
    print('Execution time: ', (end - start), 's', sep='')


if __name__ == '__main__':
    main()

Execution time: 0.08439040184020996s

@huicod
Copy link

huicod commented May 29, 2024

可以让所有进程先开启后进入等待状态,再等待主进程去同时启动

@user654321
Copy link

user654321 commented May 29, 2024 via email

@kenwoo666
Copy link

kenwoo666 commented May 29, 2024 via email

@leo97545
Copy link

leo97545 commented May 29, 2024 via email

3 similar comments
@tangwushuang
Copy link

tangwushuang commented May 29, 2024 via email

@mysmomo
Copy link

mysmomo commented May 29, 2024 via email

@Twist-echo
Copy link

Twist-echo commented May 29, 2024 via email

@moffcics
Copy link

moffcics commented May 29, 2024 via email

@RollerCoaste
Copy link

RollerCoaste commented May 29, 2024 via email

@upshalks
Copy link

upshalks commented May 29, 2024 via email

@peak-up-fsl
Copy link

peak-up-fsl commented May 29, 2024 via email

@beatricejhx
Copy link

beatricejhx commented May 29, 2024 via email

@Seraphxy
Copy link

Seraphxy commented May 29, 2024 via email

@894221671
Copy link

894221671 commented May 29, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests