Python3 plus C # concurrent programming! Powerful combination! What kind of chemical reaction does it produce?

Source: Internet
Author: User

1. Process Chapter

Official Document: Https://docs.python.org/3/library/multiprocessing.html

1.1. Progress (process)

Python process creation is very convenient, see a case: (This method is common, fork only applies to the Linux system)

Incoming group: 548377875 to get dozens of sets of PDFs Oh!

Other parameters can refer to the source code or documentation, paste the source of the Init method:

Def__init__ (self,group=none,target=none,name=none,args= (), Kwargs={},*,daemon=none)

Extension: Name: Alias for the current process instance

    1. P.is_alive () Determine if the process instance P is still executing
    2. P.terminate () terminate process (send sigterm signal)

The above case is done with OOP: (If you do not specify a method, the default is the Run method)

1.1. Source code Development

Now talk about some of the doorways inside (just like you can ignore them)

The new version of the package may be multi-layered, this time can see the python3.3.x series (this is a Python3 earlier version, a lot of code is exposed, more straightforward)

A simple description of the assertion: (Don't flood)

If the condition is true, it does nothing, whereas it triggers a assertionerror with optional error messages

1.2. Process Pool

Multiple processes do not need to manage their own manually, there is a pool to help you complete, first look at a case:

Diagram: (Join can specify timeout time, Eg:p.join (1))

You must call Close () before calling join (), and you cannot continue adding new process after calling Close ().

1.3. Source code Development

Verify that the default size of the pool is the CPU's number of cores, see source code:

multiprocessing.pool.py

Let's look at an example: (Like the JQ)

Everyone in such a hurry, then push net first, Python after a few days to push

In fact, the inverse of the day now coding has become 80% python, 20% is net, is not sure whether the net has been dry, so only as far as possible in the new knowledge, as far as possible in the brain net related content to teach you, In case of cross-industry is also worthy of everyone's love (this I never forced, anyway, what programming language is the same, let its natural ~)

Netcore Concurrent Programming

Example code: https://github.com/lotapp/BaseCode/tree/master/netcore/4_Concurrency

First of all, the concept (in fact, before there is said, so briefly):

Then add the question of the task exception, and when you await it, if an exception is thrown, capture the processing at the first await

If async and await are not understood, you can think of it this way: async is for the await to take effect (for backwards compatibility)

When an async method is called by an await, it returns to its original context when it resumes running.

If your task no longer requires context, you can use: task. Configureawait (false), eg: what's the context of writing a diary?

The inverse of the proposal is: in the core code of a use Configureawait, user page related code, do not need to add context

In fact, if there are too many await in the context of recovery that is also the comparison card, after the use of configureawait, is suspended after the online pool will continue to run

Look at another scenario: for example, a time-consuming operation, I need to specify its timeout:

Asynchronous this simple review will not say, leave two extensions, you explore:

    1. Progress can be used iprogress<t> just leave a job and find out for yourself.
    2. Avoid using tasks as much as possible after using async. Wait or task. Result, which prevents deadlocks

Other new features of task go to the official website to see it, the introduction ends here.

2. Parallel Programming (Parallel)

This actually came out for a long time, and now it's basically using PLINQ to compare more points, mainly:

    1. Data parallelism: Focus on processing data (eg: aggregation)
    2. Task parallelism: Focus on performing tasks (each task block is as independent as possible and the more independent and efficient)

Data parallelism

It used to be parallel.foreach, and now it's easy to combine it with LINQ. AsParallel () It's OK.

Say very abstract look at a simple case:

Of course, if you are in order to have the requirements can be used:. AsOrdered ()

Do not use parallelism: (a little bit more, the more CPU-intensive the greater the difference)

    1. 499999500000
    2. Real
    3. 0m0.103s
    4. User
    5. 0m0.092s
    6. Sys
    7. 0m0.021s

In fact, aggregation has a common approach that can support complex aggregations: (Take the sum above as an example)

    1. .
    2. Aggregate
    3. (
    4. Seed
    5. :
    6. 0
    7. ,
    8. Func
    9. :(
    10. Sum
    11. ,
    12. Item
    13. ) =
    14. Sum
    15. +
    16. Item
    17. );

A little extension, and PLINQ also supports cancellation. WithCancellation (CancellationToken)

The usage of token is the same as above, and if you need to combine it with async, a task.run can hand the parallel task to the thread pool.

You can also use the Async method of a task to set the time-out so that PLINQ expires and then terminates

PLINQ is so convenient, in fact, there are some minor drawbacks, such as it will directly maximize the use of system resources, may affect other tasks, while the traditional parallel will dynamically adjust

Task parallelism (parallel invocation)

This PLINQ does not seem to have a corresponding method, there is a new syntax you can say, to give an example:

Python3 plus C # concurrent programming! Powerful combination! What kind of chemical reaction does it produce?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.