多线程无法正常工作

时间:2017-01-31 09:38:58

标签: python pyqt4 python-multithreading

我正在使用PyQt4和Python 2.7。

我有两个功能可以同时启动。但第二个功能直到第一个结束才开始。

出了什么问题?

use Modern::Perl;
use Mail::POP3Client;
use MIME::QuotedPrint;

my $pop_user = 'XXXXXXXXXX';
my $pop_pass = 'XXXXXXXXXX';
my $pop_host = 'exchange3';

#connect to POP3 sever
my $pop = new Mail::POP3Client    ( HOST => $pop_host );
$pop->User($pop_user);
$pop->Pass($pop_pass);
$pop->Connect() 
    or die "Unable to connect to POP3 server: ".$pop->Message()."\n";

#count number of items in POP3 mailbox                                
my $mailcount = $pop->Count();

for (my $i = 1; $i <= $mailcount ; $i++) {
    my $header = $pop->Head($i); #gets the header
    my $uni = $pop->Uidl($i); # gets the unquie id
    my $body = $pop->Body($i);
    $body = decode_qp($body); #decode quoted printable body
    say "$uni";
    say "$header\n";
    say "$body";
}

我以这种方式调用函数:

def testvision():
    x=5
    while x>0:
        print 'vision'
        time.sleep(1)
        x=x-1
    print 'finish vision'

def testforword():
    x=5
    while x>0:
        print 'froword'
        time.sleep(1)
        x=x-1
    print 'finish forword'

def Forword_thread(self):

    t1 = threading.Thread(target=testvision())
    t2 = threading.Thread(target=testforword())
    t1.start()

    t2.start()
    t1.join()
    t2.join()

enter image description here

1 个答案:

答案 0 :(得分:0)

你创建这样的线程:

t1 = threading.Thread(target=testvision())

相当于:

target = testvision()                 # returns None, so target is None now
t1 = threading.Thread(target=target)  # passes None as target

这意味着函数testvision()在当前线程中执行,新线程使用空目标方法创建,这与使用Thread()相同。启动时,该线程将调用其(空)run()方法并立即退出。

正确的方法是使用

t1 = threading.Thread(target=testvision)

t2相同。