CN107577454A - A kind of application program multithreading running optimizatin method based on python - Google Patents
A kind of application program multithreading running optimizatin method based on python Download PDFInfo
- Publication number
- CN107577454A CN107577454A CN201710874653.6A CN201710874653A CN107577454A CN 107577454 A CN107577454 A CN 107577454A CN 201710874653 A CN201710874653 A CN 201710874653A CN 107577454 A CN107577454 A CN 107577454A
- Authority
- CN
- China
- Prior art keywords
- thread
- python
- application program
- program
- method based
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 45
- 230000007246 mechanism Effects 0.000 claims description 3
- 238000005538 encapsulation Methods 0.000 claims 1
- 208000010513 Stupor Diseases 0.000 abstract description 3
- 230000006870 function Effects 0.000 description 16
- 230000008569 process Effects 0.000 description 9
- 238000013461 design Methods 0.000 description 3
- 230000003993 interaction Effects 0.000 description 3
- 230000002452 interceptive effect Effects 0.000 description 3
- 238000012545 processing Methods 0.000 description 3
- 230000008859 change Effects 0.000 description 2
- 230000018109 developmental process Effects 0.000 description 2
- 238000012986 modification Methods 0.000 description 2
- 230000004048 modification Effects 0.000 description 2
- 230000004044 response Effects 0.000 description 2
- 238000013515 script Methods 0.000 description 2
- 230000006399 behavior Effects 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000004891 communication Methods 0.000 description 1
- 238000004590 computer program Methods 0.000 description 1
- 230000007547 defect Effects 0.000 description 1
- 238000011161 development Methods 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000008676 import Effects 0.000 description 1
- 230000008140 language development Effects 0.000 description 1
- 238000011017 operating method Methods 0.000 description 1
- 230000001052 transient effect Effects 0.000 description 1
Landscapes
- Stored Programmes (AREA)
Abstract
The invention discloses a kind of application program multithreading running optimizatin method based on python, comprise the following steps:Step 1, python threading modules are first directed to.Step 2, create one and inherit the .run () function in the thread of threading modules, rewriteeing the thread, the time-consuming operation in original main program is Resealed into the thread.Step 3, the thread pool for storing run thread is created at the main-inlet of application program, its data structure is list.Step 4, call the place of time-consuming operation to leave out its content originally, be changed to call packaged thread, the thread specified is called to open using .start () function.Step 5, wx.CallAfter modules are called to return to the data of server end to main program in specific thread.The present invention can do multiple procedure operation simultaneously, and effectively prevent program from entering torpor.
Description
Technical field
The present invention relates to Windows terminal applies security fields, and in particular to a kind of application program based on python is more
Thread running optimizatin method.
Background technology
Python is one and high-level combines explanatory, compiled, interactive and object-oriented script.
Python design has very strong readability, and english keyword, some marks of other language are commonly used compared to other language
Point symbol, it has syntactic structure more characteristic than other language.Python is a kind of interpreted languages:This means developed
Without this link of compiling in journey.Similar to PHP and Perl language.Python is interactive language:It means that you can be with
In a Python prompt, directly interactive perform writes your program.Python is object oriented language:This means Python
Support object-oriented style or code wrap object programming technique.Python is the language of beginner:Python is to first
It is a kind of great language for level programmer, it supports to be widely applied program development, from simple word processing to WWW
Browser arrives game again.
Python is in late nineteen eighties and the early 1990s, in the national mathematics of Holland and meter by Guido van Rossum
Suan Ji Science Institutes design.Python be also in itself by other many language developments and Lai, this include ABC,
Modula-3, C, C++, Algol-68, SmallTalk, Unix shell and other scripts etc..As Perl language one
Sample, Python source codes equally follow GPL (GNU General Public License) agreement.Present Python is by one
Core development teams are safeguarding that Guido van Rossum still in occupation of vital effect, instruct its progress.
Before multithreading (Multithreaded, MT) programming occurs, the operation of computer program is by a Perform sequence group
Into Perform sequence is run in the central processor CPU of main frame in order.Even if whole program is by multiple unrelated independently of each other
Subtask is formed, and program all can be performed sequentially.
Because parallel processing can significantly lift the efficiency of whole task, therefore introduce multi-thread programming.
Task has the characteristics that in multithreading:
(1) essence of these tasks is asynchronous, it is necessary to there is multiple concurrent transactions;
(2) operation order of each affairs can be uncertain, random, uncertain.
Such programmed tasks be segmented into it is multiple perform stream, each stream has the target to be completed.Further according to not
Same application, these subtasks may will calculate an intermediate result, for the result for merging to the end.
Thread (being also known as Lightweight Process) is somewhat like with process, unlike:All threads operate in same
In process, identical running environment is shared.They can be imagined as being " the fan run parallel in host process or " main thread "
Your process ".
Thread has beginning, and order performs and terminated three parts.It has an instruction pointer of oneself, records oneself and runs to
Where.The operation of thread may be preempted (interruption) or it is temporary transient be suspended (sleep), allow the operation of other threads, this is called
Give way.
Shared between each thread in one process with piece of data space, so can be than between process between thread
More easily shared data and mutually communication.What thread typically concurrently performed, it is total to just because of this parallel and data
The mechanism enjoyed causes the cooperation of multiple tasks to become possibility.It is concurrently really impossible in fact, in single CPU system
, each thread can be arranged to every time only one small meeting of operation, then just CPU is allowed out, allow other threads to go to run.
During the whole service of process, thing that each thread is only Myself, with the shared operation of other threads when needing
Result.Certainly, such share not is to be safe from danger completely.If multiple threads access same piece of data jointly,
Because the order of data access is different, it is possible to cause data result it is inconsistent the problem of, i.e. race condition (race
condition).Equally, synchronization primitives of most of thread libraries all with some row, to control the visit of the execution of thread and data
Ask.Another thing to note is the function due to having to block before completion, is not being made an amendment specifically for multithreading
In the case of, the function of this " greediness " can allow CPU time to be distributed and tilt, during the operation for causing each thread to be assigned to
Between may be not quite similar, it is fair not to the utmost.
In the Windows system applications using python compilings, program acquiescence is single thread to handle and user
Interaction and response to background server make feedback.The specific form of expression is that program can only handle current a certain thing
Part, for example downloaded or during upper transmitting file to server when these events are not belonging to time-consuming operation, user will not experience substantially
Use problem.But when when it come to program circuit time-consuming, that occupancy computer resource is excessive, program will enter seemingly-dead shape
State, the event for being currently needed for processing can only be run, this when, the function due to not opening multithreading, was inputted for new user
Or server response, program will wait current thread to be disposed completely, i.e., after current thread terminates, can just reopen line
Journey is used to handle ensuing related procedure.This means user can only single use application program certain function, it is necessary to it is right
Programming further optimizes, and as program adds the function of multiple threads event.
The content of the invention
The defects of it is an object of the invention to overcome prior art, there is provided it is a kind of can with multiple threads thing based on
Python application program multithreading running optimizatin method.
What the present invention was realized in:
A kind of application program multithreading running optimizatin method based on python, it is characterised in that methods described is included such as
Lower step:
Step 1, python threading modules are first directed to, the module to thread modules by carrying out secondary envelope
Dress, there is provided more easily API operates thread.
Step 2, create one and inherit the .run () function in the thread of threading modules, rewriteeing the thread, will be original
Time-consuming operation in main program is Resealed into the thread, while pays attention to the importing of relevant parameter.
Step 3, the thread pool for storing run thread, its data structure are created at the main-inlet of application program
For list.
Step 4, call the place of time-consuming operation to leave out its content originally, be changed to call packaged thread, use
.start () function calls the thread specified to open.
Step 5, wx.CallAfter modules are called to return to the data of server end to main program in specific thread, and
It is shown in the specified location of terminal.
Further scheme is:
It is the mechanism that its thread pool is introduced in the threading modules for importing python in the step 1.
Further scheme is:
Main program of the thread class created in the step 2 independently of application program.
Further scheme is:
Need to rewrite its initialization function (_ _ init__) in the thread class created in the step 2, will implement
Service required for parameter in this function be passed to thread in.
Further scheme is:
Specific time-consuming operation is arranged in into .run () function i.e. thread in the thread class created in the step 2 to run
In function.
Further scheme is:
The thread pool of the storage thread created in the step 3 is placed on the initialization function (_ _ init__) of main program
In.
Further scheme is:
It is the specific flow for calling thread to perform time-consuming operation in the step 4, it is comprised the following steps that:
Step 4.1, at the code of original execution time-consuming operation, the thread class before calling creates a new thread
The parameter of initialization is attached with simultaneously, wherein including original time-consuming operation.
Step 4.2, for the thread pool created among main program, will be created using .append () method in previous step
Thread be added in thread pool.
Step 4.3, specified thread is called to open using .start () method, pays attention to performing .start in main program
After () method, thread starts, and initializes and gets be passed to parameter, then performs .run () method, that is, performs time-consuming
Operation.
It should be noted that direct import is imported in the step 1, demand is not used alone pip modules and pacified
Dress.
It should be noted that it is to create a threading.Thread object in the step 2, in its initialization letter
Object can be called to be passed to as parameter in number (_ _ init__).
It should be further stated that the parameter in the step 2 is related with time-consuming operation, it is related to sending server
The parameter such as destination request.Specific parameter just needs clearly when this thread class is created.
It should be further stated that .run () method in the step 2 will perform after thread is opened, can be phase
The logic of pass is write in run methods, in this invention, has just been write on time-consuming operation in this method, once the thread quilt
Enable the logic carried out in .run ().
It should be further stated that the thread pool list created in the step 3 need not initialize to it, if
Its default value is put as sky.
It should be further stated that the calling of wx.CallAfter modules is still placed on the thread class in the step 5
In, use and the data in main program and control have been handled in wx.CallAfter module can direct-on-line journeys.
Beneficial effects of the present invention are as follows:
1. user not lock into wait program can only handle current flow, due to programming at the beginning of be have it is more
Kind function, including upload, download file etc., user can do all of above procedure operation simultaneously after having invented this patent.
2. effectively prevent that program from entering torpor, such as program is when carrying out a downloading task, if user is now
Interacted with program, can be because the number of threads blocked to be excessive, memory consumption is excessive, and program, which will enter, stops responsive state.
Brief description of the drawings
Fig. 1 is the operation interaction diagrams for using program before the invention.
Fig. 2 is to use the user interaction flow figure after the invention.
Embodiment
Below with reference to accompanying drawing, the invention will be further described, it is necessary to which explanation, the present embodiment is with the technical program
Premised on, detailed embodiment and operating procedure are provided, but protection scope of the present invention is not limited to embodiment.
It is the operational process for applying program before this invention first as shown in figure 1, when program is received from user's
When new operation, if current be still within the state of handling current request, new request will be suspended, and be waited
Current time-consuming operation is disposed, and then carries out handling new request.It should be noted that if newly requested can cause to account for more
Excessive with memory headroom, program enters torpor, it will the alternative events of user can not be responded.
Program circuit after optimizing using the present invention is as shown in Figure 2.Specific embodiment is:
Step 1, python threading modules are first directed to, the module to thread modules by carrying out secondary envelope
Dress, there is provided more easily API operates thread.
Step 2, create one and inherit the .run () function in the thread class of threading modules, rewriteeing the thread, by original
Time-consuming operation in first main program is Resealed into the thread, while pays attention to the importing of relevant parameter.
First, it is to create a threading.Thread object, can in its initialization function (_ _ init__)
Object is called to be passed to as parameter.
Secondly, parameter therein is related with time-consuming operation, is related to the parameters such as the destination request that is sent to server.Specifically
Parameter just needed when this thread class is created clearly.
Finally .run () method in thread class will perform after thread is opened, and the logic of correlation can be write run side
In method, in this invention, just time-consuming operation is write in this method, the thread, which is once activated, carries out .run
Logic in ().
Step 3, the thread pool for storing run thread, its data structure are created at the main-inlet of application program
For list.
Step 4, call the place of time-consuming operation to leave out its content originally, be changed to call packaged thread, in thread
Current thread is added in pond, the thread specified is called to open using .start () function.
First, at the code of original execution time-consuming operation, it is same that the thread class before calling creates a new thread
When be attached with the parameter of initialization, wherein including original time-consuming operation.
Secondly, for the thread pool created among main program, the line that will be created using .append () method in previous step
Journey is added in thread pool.
Finally, specified thread is called to open using .start () method, pays attention to performing .start () in main program
After method, thread starts, and initializes and gets be passed to parameter, then performs .run () method, that is, performs time-consuming behaviour
Make.
Step 5, due to needing some information into user feedback time-consuming operation, such as progress of download file etc., need
Wx.CallAfter modules are used, call the module to return to the data of server end to main program in specific thread, and
It is shown in the specified location of terminal.Wherein the calling of wx.CallAfter modules is still placed in the thread class, is made
The data in main program and control are handled with wx.CallAfter module can direct-on-line journeys.
For those skilled in the art, technical scheme that can be more than and design, provide various corresponding
Change and deform, and all these change and deformation should be construed as being included within the protection domain of the claims in the present invention.
Although reference be made herein to invention has been described for explanatory embodiment of the invention, and above-described embodiment is only this hair
Bright preferable embodiment, embodiments of the present invention are simultaneously not restricted to the described embodiments, it should be appreciated that people in the art
Member can be designed that a lot of other modifications and embodiment, and these modifications and embodiment will fall in principle disclosed in the present application
Within scope and spirit.
Claims (7)
- A kind of 1. application program multithreading running optimizatin method based on python, it is characterised in that comprise the following steps:Step 1, python threading modules are first directed to, the module carries by carrying out secondary encapsulation to thread modules More easily API has been supplied to operate thread;Step 2, create one and inherit the .run () function in the thread of threading modules, rewriteeing the thread, will original main journey Time-consuming operation in sequence is Resealed into the thread, while pays attention to the importing of relevant parameter;Step 3, the thread pool for storing run thread is created at the main-inlet of application program, its data structure is list;Step 4, call the place of time-consuming operation to leave out its content originally, be changed to call packaged thread, use .start () function calls the thread specified to open;Step 5, wx.CallAfter modules are called to return to the data of server end to main program in specific thread, and at end The specified location at end is shown to it.
- 2. the application program multithreading running optimizatin method based on python according to claim 1, it is characterised in that:It is the mechanism that thread pool is introduced in the threading modules for importing python in step 1.
- 3. the application program multithreading running optimizatin method based on python according to claim 1, it is characterised in that:Main program of the thread class created in step 2 independently of application program.
- 4. the application program multithreading running optimizatin method based on python according to claim 3, it is characterised in that:Need to rewrite its initialization function (_ _ init__) in the thread class created in step 2, by the service institute of specific implementation The parameter needed is passed in thread in this function.
- 5. the application program multithreading running optimizatin method based on python according to claim 1 or 4, it is characterised in that:Specific time-consuming operation is arranged in .run () function i.e. thread operation function in the thread class created in step 2.
- 6. the application program multithreading running optimizatin method based on python according to claim 1, it is characterised in that:The thread pool of the storage thread created in step 3 is placed in the initialization function (_ _ init__) of main program.
- 7. the application program multithreading running optimizatin method based on python according to claim 1, it is characterised in that:It is the specific flow for calling thread to perform time-consuming operation in step 4, it is comprised the following steps that:Step 4.1, at the code of original execution time-consuming operation, the thread class before calling creates a new thread simultaneously The parameter of initialization is attached with, wherein including original time-consuming operation;Step 4.2, for the thread pool created among main program, the line that will be created using .append () method in previous step Journey is added in thread pool;Step 4.3, specified thread is called to open using .start () method, pays attention to performing .start () in main program After method, thread starts, and initializes and gets be passed to parameter, then performs .run () method, that is, performs time-consuming behaviour Make.
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201710874653.6A CN107577454A (en) | 2017-09-25 | 2017-09-25 | A kind of application program multithreading running optimizatin method based on python |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201710874653.6A CN107577454A (en) | 2017-09-25 | 2017-09-25 | A kind of application program multithreading running optimizatin method based on python |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN107577454A true CN107577454A (en) | 2018-01-12 |
Family
ID=61038955
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN201710874653.6A Pending CN107577454A (en) | 2017-09-25 | 2017-09-25 | A kind of application program multithreading running optimizatin method based on python |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN107577454A (en) |
Cited By (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN108319455A (en) * | 2018-01-25 | 2018-07-24 | 北京国睿中数科技股份有限公司 | The programming methods and procedures system for writing and compiling of multithreading |
| CN108319458A (en) * | 2018-01-17 | 2018-07-24 | 南京航空航天大学 | It is a kind of based on graphically defend formula order calculation multitask Compilation Method |
| CN110609756A (en) * | 2019-08-15 | 2019-12-24 | 中国平安人寿保险股份有限公司 | Reading method, electronic device, computer equipment and storage medium |
Citations (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US7269833B2 (en) * | 2001-12-12 | 2007-09-11 | Sun Microsystems, Inc. | Scriptable plug-in application programming interface |
| CN106325980A (en) * | 2015-06-30 | 2017-01-11 | 中国石油化工股份有限公司 | Multi-thread concurrent system |
-
2017
- 2017-09-25 CN CN201710874653.6A patent/CN107577454A/en active Pending
Patent Citations (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US7269833B2 (en) * | 2001-12-12 | 2007-09-11 | Sun Microsystems, Inc. | Scriptable plug-in application programming interface |
| CN106325980A (en) * | 2015-06-30 | 2017-01-11 | 中国石油化工股份有限公司 | Multi-thread concurrent system |
Non-Patent Citations (2)
| Title |
|---|
| GOLDENSUN: "分析Python编程时利用wxPython来支持多线程的方法", 《HTTPS://WWW.JB51.NET/ARTICLE/63632.HTM》 * |
| 秋名山车神: "Python多线程之threading、Thread实现", 《HTTP://WWW.IMOOC.COM/ARTICLE/16198》 * |
Cited By (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN108319458A (en) * | 2018-01-17 | 2018-07-24 | 南京航空航天大学 | It is a kind of based on graphically defend formula order calculation multitask Compilation Method |
| CN108319455A (en) * | 2018-01-25 | 2018-07-24 | 北京国睿中数科技股份有限公司 | The programming methods and procedures system for writing and compiling of multithreading |
| CN110609756A (en) * | 2019-08-15 | 2019-12-24 | 中国平安人寿保险股份有限公司 | Reading method, electronic device, computer equipment and storage medium |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| Landin | The mechanical evaluation of expressions | |
| CN112119423B (en) | Formalized verification system and method | |
| US9250894B2 (en) | Sequentially constructive model of computation | |
| US9170857B2 (en) | Managed execution environment for software application interfacing | |
| CN106462375B (en) | Graphics Pipeline State Objects and Models | |
| US10970130B2 (en) | Composable and cancelable dataflow continuation passing | |
| US7950018B2 (en) | Synchronizing execution using a single-threaded scripting model | |
| CN109598107A (en) | A kind of code conversion method and device based on application installation package file | |
| CN107577454A (en) | A kind of application program multithreading running optimizatin method based on python | |
| US8407678B2 (en) | Method of array interception using data-flow analysis | |
| US6957415B1 (en) | Method for self-organizing software | |
| CN109426497A (en) | Data embedding method, device, system and storage medium | |
| JPH03208159A (en) | Extended software function for multiprocessor system | |
| Prokopec | Learning Concurrent Programming in Scala | |
| US8347270B1 (en) | Self-assembled software and method of overriding software execution | |
| WO1999024904A1 (en) | Apparatus and method for logging information relating to function calls to a function library | |
| CN105302579B (en) | A kind of occupancy method and apparatus of Installed System Memory | |
| US11537418B2 (en) | Simulated change of immutable objects during execution runtime | |
| CN119166920A (en) | A page rendering method, device, equipment, storage medium and program product | |
| CN109542447A (en) | A kind of OTX program file execution method and device | |
| RU2143726C1 (en) | Formula processor with instruction-like logical control gates | |
| Freeman | The singleton pattern | |
| CN107145372A (en) | information generating method and device | |
| Meijers | Unity Advanced | |
| Ciesla | Calendars, Culture, and Multithreading in C# |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| RJ01 | Rejection of invention patent application after publication | ||
| RJ01 | Rejection of invention patent application after publication |
Application publication date: 20180112 |