在线程函数中使用请求模块时,为什么会出现错误?

时间:2015-04-18 19:36:55

标签: python multithreading

我正在尝试学习多线程,也是编写python的新手,试图移植一个性情的PHP脚本......

当我跑步时,我得到错误:
Unhandled exception in thread started by <function getthis at 0x7f2156dbb0c8>

#!/usr/bin/python
import thread
import time
import sys, requests, json, urllib2

def getthis( ticker ):
   x = requests.get("http://google.com/" + ticker)
   print x
   return


# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 0, ) )
   thread.start_new_thread( getthis, ("AAPL", ) )
except:
       print "Error: unable to start thread"

while 1:
   pass

当我单独运行getthis函数时它工作正常,我想我在thread.start中称错了?请帮忙

0 个答案:

没有答案