2016-11-11 63 views
0
from flask import Flask 
from flask import request 
import threading 
import time 
from pymongo import MongoClient 
import gridfs 
bridgeVideoDatabase = MongoClient().bridgeVideoDatabase 
fs = gridfs.GridFS(bridgeVideoDatabase) 

app = Flask(__name__) 

host = '10.0.0.23' 
port = 8080 

client = MongoClient('mongodb://localhost:27017/') 
db = client.bridgeUserInformationTable 



@app.route('/sendUserStringIntData',methods=['GET', 'POST']) 
def getDataFromAndroid():  
    print "Entire Data:",request.form 
    UserName = request.form['UserName'] 
    print UserName 
    #insertion 
    db.bridgeUsersInfo.insert({"UserName":UserName}) 
    #display in console 
    cursor = db.bridgeUsersInfo.find() 
    for document in cursor: 
     print(document) 
    Thread1 = threading.Thread(target=getDataFromAndroid) 

    t=time.time() 

    Thread1.start() 
    Thread1.join() 
    print'Total time for execution :',time.time()-t,'msec' 
    return "User Info Saved successfully in Database :-)" 


if __name__ == '__main__': 
    app.run(
     host=host, port=port 
     ) 

以上是測量使用python將數據保存到數據庫以及從andoid更新輸入的時間的腳本。雖然運行相同它是託管,但在保存文本後,它給以下錯誤使用Python中的多線程測量執行時間

 * Running on http://10.0.0.23:8080/ (Press CTRL+C to quit) 
[2016-11-11 11:52:22,136] ERROR in app: Exception on /sendUserStringIntData [POST] 
Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app 
    response = self.full_dispatch_request() 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception 
    reraise(exc_type, exc_value, tb) 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
TypeError: getDataFromAndroid() takes exactly 1 argument (0 given) 
10.0.0.27 - - [11/Nov/2016 11:52:22] "POST /sendUserStringIntData HTTP/1.1" 500 

它應該如何解決?

* Running on http://10.0.0.23:8080/ (Press CTRL+C to quit) 
Entire Data: ImmutableMultiDict([('UserName', u'mahaveer'), ('Age', u'0'), ('Password', u'null')]) 
mahaveer 
{u'UserName': u'mahaveer', u'Age': u'24', u'_id': ObjectId('5825950e0f325f3a30ef7292'), u'Password': u'12345'} 
{u'UserName': u'mahaveer', u'_id': ObjectId('582595530f325f3a563681c7')} 
Entire Data:Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner 
    self.run() 
    File "/usr/lib/python2.7/threading.py", line 763, in run 
    self.__target(*self.__args, **self.__kwargs) 
    File "/home/cosmitude10/neon/measureMultithread/test.py", line 22, in getDataFromAndroid 
    print "Entire Data:",request.form 
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 343, in __getattr__ 
    return getattr(self._get_current_object(), name) 
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 302, in _get_current_object 
    return self.__local() 
    File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 37, in _lookup_req_object 
    raise RuntimeError(_request_ctx_err_msg) 
RuntimeError: Working outside of request context. 

This typically means that you attempted to use functionality that needed 
an active HTTP request. Consult the documentation on testing for 
information about how to avoid this problem. 

Total time for execution : 0.00076699256897 msec 
10.0.0.27 - - [11/Nov/2016 15:24:27] "POST /sendUserStringIntData HTTP/1.1" 200 - 
+0

錯誤看起來並不像它的持續時間檢查 –

+0

發生的事情我沒有得到什麼確切的錯誤的意思是,我叫timeit內置模塊, python新增 – mad

+0

將定時過程更改爲燒瓶處理程序。你的處理程序是無主的! Therad不幫助你,如果你在完成everythigs之後打電話! – dsgdfg

回答

0

therading

threading.Thread(group=None, target=None, name=None, args=(), kwargs={}) 

args is the argument tuple for the target invocation. Defaults to()

t_args = (1) 
assert type(t_args) is tuple, "except tuple, get {0}".format(type(t_args)) 
>>> AssertionError: except tuple, get <class 'int'> 

t_args = (request,) 
print type(t_args) 
>>> <class 'tuple'> 

t= threading.Thread(target=getDataFromAndroid, args=t_args) 

元組ARGS =()/列表ARGS = []
這裏也可以使用部分從functools與ARGS

調用
class threading.Timer(interval, function, args=[], kwargs={}) 
threading.Timer(1, partial(your_method, your_arg_a, your_arg_b)).start() 

This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times

import timeit 
.... 
start = timeit.default_timer() 
t.start() 
t.join() 
print timit.default_timer - start 
+0

沒有得到確切的東西我需要 – mad

+0

我通過android傳遞值,我應該如何將該參數值傳遞給線程? – mad

+0

你的意思是「請求」參數? –