Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of a C# Task in python

Tags:

python

c#

task

I need to translate my c# code to python. I have a Task in c# and need to translate it to python. Here is a piece of the code:

List<Task> t = new List<Task>();
for(int i = 0; i < _tasks.Count; i++)
{
     var theTask = _tasks[i];
     t.Add(Task.Factory.StartNew(() => theTask.SAPTask.Execute(theTask.index, theTask.WindowCount)));
}
t.ForEach(x => x.Wait());

This little piece of code is crucial to run my program. I need python to run all the tasks in the list in separate threads, and block the main thread until all the tasks are completed. Does python have this functionality?

like image 497
Luke101 Avatar asked Jun 05 '13 23:06

Luke101


People also ask

What is equivalent to classes in C?

The closest thing you can get is a struct .

Is C+ Same as C?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language. C is (mostly) a subset of C++.

What is equivalent to p >= q?

P→Q is logically equivalent to ⌝P∨Q. So. ⌝(P→Q) is logically equivalent to ⌝(⌝P∨Q).

Is there an alternative to C?

The best alternative is Java. It's not free, so if you're looking for a free alternative, you could try C++ or Rust. Other great apps like C (programming language) are Go (Programming Language), C#, Lua and Perl.


1 Answers

I need Python to run all the tasks in the list and then block the main thread until all the tasks are completed.

The main use case for Task is to avoid blocking the main thread, so the user can interact with application while the work is pushed to different threads.

Are you just trying to implement parallelism between individual pieces of work?
If you're using Python 3.2, there is concurrent.futures that does look like TPL in a way.

like image 184
Dan Abramov Avatar answered Sep 23 '22 12:09

Dan Abramov