鱼C论坛

 找回密码
 立即注册
查看: 1597|回复: 10

[已解决]一个很弱的问题。。。python多线程用不了

[复制链接]
发表于 2017-10-18 18:24:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写了一个最基本的多线程代码。。。可是打印出来确是下面这个样子的,十分不解,求大牛解答

  1. import threading
  2. import time

  3. # 为线程定义一个函数
  4. def target(threadName, delay):
  5.     count = 0
  6.     while count < 5:
  7.         time.sleep(delay)
  8.         count += 1
  9.         print(threadName + time.ctime(time.time()))
  10. # 创建多个线程
  11. t1 = threading.Thread(target=target("First threads",1))
  12. t2 = threading.Thread(target=target("Second threads",1))
  13. t1.start()
  14. t1.join()
  15. t2.start()
  16. t2.join()
复制代码


First threadsWed Oct 18 18:17:51 2017
First threadsWed Oct 18 18:17:52 2017
First threadsWed Oct 18 18:17:54 2017
First threadsWed Oct 18 18:17:55 2017
First threadsWed Oct 18 18:17:56 2017
Second threadsWed Oct 18 18:17:57 2017
Second threadsWed Oct 18 18:17:58 2017
Second threadsWed Oct 18 18:17:59 2017
Second threadsWed Oct 18 18:18:00 2017
Second threadsWed Oct 18 18:18:01 2017
>>>

最佳答案
2017-10-18 19:42:08
  1. t1 = threading.Thread(target=target, args=("First threads",1))
  2. t2 = threading.Thread(target=target, args=("Second threads",1))
  3. t1.start()
  4. t2.start()
  5. t1.join()
  6. t2.join()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-18 18:38:52 | 显示全部楼层
t1.start()
t2.start()
t1.join()

t2.join()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-18 19:18:10 | 显示全部楼层
SixPy 发表于 2017-10-18 18:38
t1.start()
t2.start()
t1.join()

很遗憾,没有用

  1. import threading
  2. import time

  3. # 为线程定义一个函数
  4. def target(threadName, delay):
  5.     count = 0
  6.     while count < 5:
  7.         time.sleep(delay)
  8.         count += 1
  9.         print(threadName + time.ctime(time.time()))
  10. # 创建多个线程
  11. t1 = threading.Thread(target=target("First threads",1))
  12. t2 = threading.Thread(target=target("Second threads",1))
  13. t1.start()
  14. t2.start()
  15. t1.join()
  16. t2.join()
复制代码


First threadsWed Oct 18 19:16:39 2017
First threadsWed Oct 18 19:16:40 2017
First threadsWed Oct 18 19:16:41 2017
First threadsWed Oct 18 19:16:42 2017
First threadsWed Oct 18 19:16:43 2017
Second threadsWed Oct 18 19:16:44 2017
Second threadsWed Oct 18 19:16:45 2017
Second threadsWed Oct 18 19:16:46 2017
Second threadsWed Oct 18 19:16:47 2017
Second threadsWed Oct 18 19:16:48 2017
>>>

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-18 19:42:08 | 显示全部楼层    本楼为最佳答案   
  1. t1 = threading.Thread(target=target, args=("First threads",1))
  2. t2 = threading.Thread(target=target, args=("Second threads",1))
  3. t1.start()
  4. t2.start()
  5. t1.join()
  6. t2.join()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-10-18 20:07:18 | 显示全部楼层

threading.Thread(target, args=("First threads",1))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-18 20:46:39 | 显示全部楼层
怎么样的代码 才是多线程?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-18 20:50:16 | 显示全部楼层
SixPy 发表于 2017-10-18 20:07
threading.Thread(target, args=("First threads",1))

谢谢!成功了~~原来只有这种写法才行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-18 20:53:20 | 显示全部楼层

谢谢版主~~~
现在又出现了这么一个问题

First threadsWed Oct 18 20:46:48 2017Second threadsWed Oct 18 20:46:48 2017

First threadsWed Oct 18 20:46:49 2017Second threadsWed Oct 18 20:46:49 2017

First threadsWed Oct 18 20:46:50 2017Second threadsWed Oct 18 20:46:50 2017

First threadsWed Oct 18 20:46:51 2017Second threadsWed Oct 18 20:46:51 2017

First threadsWed Oct 18 20:46:52 2017Second threadsWed Oct 18 20:46:52 2017

>>>

居然是隔行输出,可我并没有录入换行符,不知道为什么不能实现之前的排版
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-18 20:54:09 | 显示全部楼层
要学习 发表于 2017-10-18 20:46
怎么样的代码 才是多线程?

你可以看看八楼,应该是一样一行轮换输出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-18 21:00:22 | 显示全部楼层
xiewen1235 发表于 2017-10-18 20:53
谢谢版主~~~
现在又出现了这么一个问题

代码呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-18 22:31:44 | 显示全部楼层

就是按照之前说的,修改了线程的定义,结果出来的效果如下
  1. import threading
  2. import time

  3. # 为线程定义一个函数
  4. def target(threadName, delay):
  5.     count = 0
  6.     while count < 5:
  7.         time.sleep(delay)
  8.         count += 1
  9.         print(threadName + time.ctime(time.time()))
  10. # 创建多个线程
  11. t1 = threading.Thread(target=target, args=("First threads",1))
  12. t2 = threading.Thread(target=target, args=("Second threads",1))
  13. t1.start()
  14. t2.start()
  15. t1.join()
  16. t2.join()
复制代码



First threadsWed Oct 18 22:29:49 2017Second threadsWed Oct 18 22:29:49 2017

First threadsWed Oct 18 22:29:50 2017Second threadsWed Oct 18 22:29:50 2017

First threadsWed Oct 18 22:29:51 2017Second threadsWed Oct 18 22:29:51 2017

First threadsWed Oct 18 22:29:52 2017Second threadsWed Oct 18 22:29:52 2017

First threadsWed Oct 18 22:29:53 2017Second threadsWed Oct 18 22:29:53 2017

>>>

两个线程一起输出,但是又连着换行两次,格式不是很好,期望输出应该是一个一行的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 16:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表