python中使用多进程进行任务

1.进程测试1

import os
import threading

# def echo_number():
#     for i in range()


import time
import threading
def dance():
    for i in range(5):
        print("---dancing---")
        time.sleep(1)
        print(time.ctime())
def sing():
    for i in range(5):
        print("---singing---")
        time.sleep(1)
        print(time.ctime())
def main():
    t1 = threading.Thread(target = dance)
    t2 = threading.Thread(target = sing)
    t1.start()
    t2.start()
if __name__ == '__main__':
    main()

2.test2

import time
import threading
def echo_sen(sentence):
    print(sentence)
    time.sleep(1)
    print(time.ctime())
    # for i in range(5):
    #     print("---dancing---")
    #     time.sleep(1)
    #     print(time.ctime())
# def sing():
#     for i in range(5):
#         print("---singing---")
#         time.sleep(1)
#         print(time.ctime())
echo_sen('sentence')

list1=['hello','bye']

# def step1():
# if __name__ == '__main__':
#     t1 = threading.Thread(target = echo_sen,args=('hello'))
#     t2 = threading.Thread(target = echo_sen,args=('bye'))
#     t1.start()
#     t2.start()
#     echo_sen('hello')
#     echo_sen('bye')

for item in list1:
    sub_thread=threading.Thread(target=echo_sen,args=(item,))
    sub_thread.start()


    # t1 = threading.Thread(target=echo_sen, args=('hello'))
    # t1.start()



# if __name__ == '__main__':
#     main()

3.real_use

import time
import threading
def echo_sen(sentence):
    print(sentence)
    time.sleep(1)
    print(time.ctime())

list1=['hello','bye']

if __name__=='__main__':
    for item in list1:
        sub_thread=threading.Thread(target=echo_sen,kwargs={'sentence':item})
        sub_thread.start()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值