Home > other >  Everyone a great god, self-study python, now want to complete a function, and optimization.
Everyone a great god, self-study python, now want to complete a function, and optimization.

Time:09-22

Everyone a great god, self-study of python, now want to complete a function, the basic functions can be realized,
But always feel database operations this is a piece of investing resources, want to let the script has been stable operation, and optimization,


Script function: every second query a database, and found that the new data, take back, submitted to the address in the format of the API,



The import pymysql
The import time
The import requests
The import bs4




Oadun_url=""
Last_id=0
Conunt_id=""
Conunt_name=""
Conunt_symbol=""
Conunt_type=""
Conunt_openprice=""
Conunt_ticket=""
Conunt_time=""

MSG="" # order content



# get cursor

# query order
While 1:

Connect=pymysql. Connect (host="localhost", user="root", "=", "database=", "charset=" utf8 ")
Print (connect)
Cursor=connect. Cursor ()
SQL="SELECT * FROM order_info order by id desc limit 1"
Cursor. The execute (SQL)
The date=cursor. # fetchall () returns the tuples field values

Date2=date [0]
Print (date2)
Conunt_id=date2 [0]
Print (conunt_id)
If (conunt_id & gt; Last_id) : # found new orders
# get order content
Conunt_name=date2 [1]
Conunt_symbol=date2 [2]
Conunt_type=date2 [3]
Conunt_openprice=date2 [4]
Conunt_ticket=date2 [5]
Conunt_time=date2 [6]
MSG=STR (conunt_id) + "strategy name:" + conunt_name + "currency open:" + conunt_symbol + + conunt_type "order type:" + "open warehouse price:" + conunt_openprice + "order number:" + "open time:" + + conunt_ticket conunt_time
Print (MSG)
Oadun_url="HTTP://https://api.xxx.org/sendMessag& Text=strategy name: "+ conunt_name +" % 0 a currency open: "+ conunt_symbol + % 0 a order type" : "+ conunt_type +" % 0 a price open: "+ conunt_openprice +" % 0 a order number: "+ conunt_ticket +" % 0 a time: open "+ conunt_time
Print (oadun_url)

The response=requests. Get (oadun_url)
Status_code=response. Status_code
Print (status_code)
Last_id=conunt_id


Cursor. The close ()
Connect the close ()
Time. Sleep (5)
Print (" waiting ")

CodePudding user response:

Every second to query a database frequency can be a bit high,
By definition, the database write operations must be less than read (query) operation number!

If you want to improve their efficiency of program, you should first understand a problem:
You what on earth is the frequency of a database write operations?
Is 1 seconds at a time or 10 seconds at a time, or 1 minutes, or longer?

Imagine if you write the operating frequency is 1 time for 1 minute, then your query operation also can change for 1 minute 1 times!
=================

In addition, if the reading and writing database operations are controlled by yourself, so the easiest way to promote efficiency is:
When you to write operation, one of the memory directly to the agreed upon the state of the variables changes, and query operations still can use the 1 1 second monitor the corresponding change of state variables, if the status change, go for database query operation! It can also reduce the operation of the database and to promote the program access efficiency!



CodePudding user response:

reference 1st floor paullbm response:
every second query a database frequency can be a bit high,
By definition, the database write operations must be less than read (query) operation number!

If you want to improve their efficiency of program, you should first understand a problem:
You what on earth is the frequency of a database write operations?
Is 1 seconds at a time or 10 seconds at a time, or 1 minutes, or longer?

Imagine if you write the operating frequency is 1 time for 1 minute, then your query operation also can change for 1 minute 1 times!
=================

In addition, if the reading and writing database operations are controlled by yourself, so the easiest way to promote efficiency is:
When you to write operation, one of the memory directly to the agreed upon the state of the variables changes, and query operations still can use the 1 1 second monitor the corresponding change of state variables, if the status change, go for database query operation! It can also reduce the operation of the database and to promote the program access efficiency!
thank you, I write to the database is not fixed, and write to gathering information conditions, but with a memory variable database query is reduced

CodePudding user response:

refer to the second floor qq_37402318 response:
thank you, I write to the database is not fixed, and write to gathering information conditions, but with a memory variable database query is reduced


Then according to the completion of gathering information to set up a memory variables, to make the changes to the database, query again (but at the same time, the query results in the cache),
Didn't write operation, read cache!
Cache you can understand as some variables in memory or data set.

Conclusion:
Haven't update a data update, query the database from the cache query!
  • Related