关于线程的问题 [英] Question about thread
问题描述
参考我发现的以下代码,
控制台中没有显示任何内容,请问为什么?
def thrd(param): #the thread worker function
print" Received",param
import thread
for i in range(5): #start five threads将i传递给每一个
thread.start_new_thread(thrd,(i,))
提前致谢
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?
def thrd(param): # the thread worker function
print "Received",param
import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))
Thanks in advance
推荐答案
更确切地说,我想做的是让一个线程程序同时处理某些工作(而这些工作正在获取信息)来自
互联网,因此如果网络速度缓慢,线程可以加快这个过程。
start
| (blahblahblah ......)
v
+ ----- + ----- + ----- + ----- +
| | | | |
- + - - + - - + - - + - - + -
| | | | | | | | | |
| A | | B | | C | | D | | E |
| | | | | | | | | |
- + - - + - - + - - + - - + -
| | | | |
+ ----- + ----- + ----- + ----- +
| (blahblahblah ...)
v
完成!
虽然进程AE将访问同一程序中的共享变量,并且
每个进程都必须自己解析一个文件。
Valkyrie写道:
To be more precise, what I want to do is to have a threaded program to handle
some jobs concurrently (while those jobs are fetching information from the
Internet, so threading could speed up the process if the network is slow)
start
| (blahblahblah...)
v
+-----+-----+-----+-----+
| | | | |
--+-- --+-- --+-- --+-- --+--
| | | | | | | | | |
| A | | B | | C | | D | | E |
| | | | | | | | | |
--+-- --+-- --+-- --+-- --+--
| | | | |
+-----+-----+-----+-----+
| (blahblahblah...)
v
finish!
While process A-E will access the shared variables within the same program, and
each of those processes will have to parse a document on its own.
Valkyrie wrote:
参考我发现的以下代码,那里在
控制台中没有任何显示,请问为什么?
def thrd(param):#thread worker function
print" Received",param
导入线程
for range in range(5):#start five threads将i传递给每一个
thread.start_new_thread(thrd,(i,))
提前致谢
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?
def thrd(param): # the thread worker function
print "Received",param
import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))
Thanks in advance
" Valkyrie" < VA ****** @ cuhk.edu.hk>在消息中写道
news:1100875373.911763@eng-ser6 ...
"Valkyrie" <va******@cuhk.edu.hk> wrote in message
news:1100875373.911763@eng-ser6...
参考我发现的以下代码,
控制台中没有显示任何内容,我问为什么?
def thrd(param):#the thread worker function
print" Received",param
import thread
for i在范围(5)中:#start五个线程将i传递给每个人
thread.start_new_thread(thrd,(i,))
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?
def thrd(param): # the thread worker function
print "Received",param
import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))
你可能会问,但是当我尝试你的代码时,发生了什么:
Python 2.3.4(#53,2004年5月25日,21:17:02)[MSC v.1200 32 bit(英特尔)]
win32
输入help,copyright,credit等等。或许可证或更多信息。
You may ask, but when I tried your code, here is what happened:
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
def thrd(param):#thread worker function
.... print"收到,param
....导入线程
我在范围内(5):#start five threads将i传递给每一个
.... thread.start_new_thread (thrd,(i,))
....
1960
836
232
2864
3692收到0
def thrd(param): # the thread worker function .... print "Received",param
.... import thread
for i in range(5): # start five threads passing i to each one .... thread.start_new_thread(thrd,(i,))
....
1960
836
232
2864
3692 Received 0
收到1
收到2
收到3
收到4
请注意,1960等数字似乎是
thread.start_new_thread()函数。
-
我实际上并没有读过我的hotmail帐户,但你可以用
很兴奋。
Received 1
Received 2
Received 3
Received 4
Note that the numbers 1960, etc., appear to be the return values of the
thread.start_new_thread() function.
--
I don''t actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
当我在python的控制台中逐行执行时,我得到了类似的结果。
但是当我尝试在文件中运行它时,请说:
python demo.py
它只是没有回报我。我现在不知道这个......
Russell Blau写道:
When I do it line by line in python''s console, I have similar result to youl.
But when I try to run it in a file, say:
python demo.py
It just returns me nothing. I have no idea on this right now...
Russell Blau wrote:
" Valkyrie" < VA ****** @ cuhk.edu.hk>在消息中写道
新闻:1100875373.911763@eng-ser6 ...
"Valkyrie" <va******@cuhk.edu.hk> wrote in message
news:1100875373.911763@eng-ser6...
参考我发现的以下代码,
控制台中没有显示任何内容,我问为什么?
def thrd(param):#the thread worker function
print" Received",param
import thread
for i在范围(5)中:#start五个线程将i传递给每个人
thread.start_new_thread(thrd,(i,))
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?
def thrd(param): # the thread worker function
print "Received",param
import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))
你可能会问,但是当我尝试你的代码,发生了什么:
Python 2.3.4(#53,2004年5月25日,21:17:02)[MSC v.1200 32位(英特尔)]
win32
输入help,copyright,credit等等。或许可证或有关更多信息。
You may ask, but when I tried your code, here is what happened:
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
def thrd(param):#thread worker function
... print" Received",param
...
导入线程
for range in range(5):#start five threads将i传递给每一个
... thread.start_new_thread(thrd,(i,) )
...
1960
836
232
2864
3692
收到0
def thrd(param): # the thread worker function
... print "Received",param
...
import thread
for i in range(5): # start five threads passing i to each one
... thread.start_new_thread(thrd,(i,))
...
1960
836
232
2864
3692
Received 0
>
收到1
收到2
收到3
收到4
注意数字1960等等,似乎是返回值
thread.start_new_thread()函数。
Received 1
Received 2
Received 3
Received 4
Note that the numbers 1960, etc., appear to be the return values of the
thread.start_new_thread() function.
这篇关于关于线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!