# # Thread callback `` `Python 1 , create a thread object, thread object to perform a task function: task, parameter task is a callback function call_back, the callback function parameter is the results of task. DEF call_back (RES): Print ( " task result got:% S " % RES) DEF task (the callback): # Global RES Print ( " RUN " ) the time.sleep ( . 1 ) # # 100 return RES = 100 # represent results of the task the callback (RES) # perform The callback function and pass the result of the task t = the Thread (target = task, args = (Parser,)) t.start () Print( " Over " ) `` ` # # thread queue ` `` python threads in the queue, there are three: first: q = Queue () q.put () q.get () to use with the process Joinablequeue the method of the same, but do not have the IPC second: Q = LifoQueue () q.put () q.get () LIFO, last out, the analog storage stack third: Q = PriorityQueue () Q. PUT () q.get () includes a priority queue, compare the size of storage objects need to be, the smaller the higher the priority comparison, comparison operators custom objects can not be used, it can not be stored, but may cover the loading operator, as follows: class A (Object): DEF the __init__ (Self, Age): self.age= Age # DEF __lt __ (Self, OTHER): # return self.age <other.age # DEF __gt __ (Self, OTHER): # return self.age> other.age DEF __eq__ (Self, OTHER): return self.age == other.age A1 = a (50 ) A2 = a (50 ) Print (A1 == A2) `` ` # # events ` `` Python boot_event = the event () # boot_event.clear () return status to event False # boot_event.is_set () returns the state of the event #boot_event.wait () waits for the event, is to wait for the event is set to True # boot_event.set () event is set to True DEF boot_server (): Print ( " Starting server ...... " ) the time.sleep ( 3 ) Print ( " ! server started successfully " ) boot_event.set () # tag event has occurred DEF connect_server (): boot_event.wait () # wait for the event, need not be provided if the judge telling the truth, here is a blocking method. Print ( " linked server successfully! " ) T1 = the Thread (target = boot_server) t1.start () T2 = the Thread (target =connect_server) t2.start () `` ` # # threaded concurrent achieve the yield ` `` Python # using the generator to a plurality of single-wire concurrent tasks Import Time DEF func1 (): A =. 1 for I in Range (10000000 ): A + =. 1 Print ( " A RUN " ) the time.sleep ( 10 ) the yield DEF func2 (): RES = func1 () A =. 1 for I in Range (10000000 ): A + =. 1 Print ( " B RUN " ) next (RES) # fetch yield value by the next method. ST = the time.time () func2 () Print (the time.time () - ST) `` ` # # Module greenlet `` `python create greenlet object parameter is a function of task execution, by " greenlet objects " .switch (), jump to perform tasks function of the object. If there are multiple switch (), the last recorded position, from the second end position started down. Import greenlet Import time DEF task1 (): Print ( " task1 RUN " ) g2.switch () Print ("task1 over") g2.switch() def task2(): print("task2 run") g1.switch() time.sleep(2) print("task2 over") g1 = greenlet.greenlet(task1) g2 = greenlet.greenlet(task2) g1.switch() # g2.switch() print("主 over") ``` ## gevent模块 ```python #After gevent does not have the ability to detect the IO need to patch to patch as it can detect IO # Note that patch must fight at the top must ensure that it patched before you import the module from gevent Import Monkey monkey.patch_all () # after the patch, not all IO can identify, within a specific patch package to see what IO. from Threading Import current_thread Import gevent, Time DEF task1 (): Print (current_thread (), 1 ) Print ( " task1 RUN " ) # gevent.sleep (. 3) the time.sleep (. 3 ) Print ( " Task1 over " ) DEFTask2 (): Print (current_thread (), 2 ) Print ( " Task2 RUN " ) Print ( " Task2 over " ) # the spawn is used to create a task coroutine G1 = gevent.spawn (Task1) G2 = gevent.spawn (Task2 ) # tasks to be performed, must ensure that the main thread did not hang because all coroutine main tasks are executed, it must be called to join in the task wait coroutines # g1.join () # g2.join () # theoretically awaiting execution time most long mission on the line, but it is unclear who may be a long time all the Join gevent.joinall ([g1, g2]) Print ( " over " ) patched implementation principles: IO method is covered by a non-blocking blocking change. ```